创建Bitmap之Bitmap静态方法使用示例

package com.loaderman.customviewdemo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //创建白色透明渐变图像
        findViewById(R.id.bitmap_blank_gradient).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, BmpBlankGradientActivity.class));
            }
        });

        //裁剪图像
        findViewById(R.id.bitmap_crop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cutImage();
            }
        });

        //剪图像并用Matrix
        findViewById(R.id.bitmap_crop_matrix).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cutImageMatrix();
            }
        });

        //指定色彩创建图像
        findViewById(R.id.bitmap_colors).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createBmpByColors();
            }
        });

        //createScaledBitmap
        findViewById(R.id.bitmap_scaled).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createScaledBitmap();
            }
        });
    }


    //裁剪图像
    private void cutImage() {

        Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.dog);
        Bitmap cutedBmp = Bitmap.createBitmap(src, src.getWidth() / 3, src.getHeight() / 3, src.getWidth() / 3, src
                .getHeight() / 3);

        ImageView iv = (ImageView) findViewById(R.id.bmp_img);
        iv.setImageBitmap(cutedBmp);
    }

    //裁剪图像并用Matrix
    private void cutImageMatrix() {
        Matrix matrix = new Matrix();
        matrix.setScale(2, 1);

        Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.dog);
        Bitmap cutedBmp = Bitmap.createBitmap(src, src.getWidth() / 3, src.getHeight() / 3, src.getWidth() / 3, src
                .getHeight() / 3, matrix, true);

        ImageView iv = (ImageView) findViewById(R.id.bmp_img2);
        iv.setImageBitmap(cutedBmp);
    }

    //指定色彩创建图像
    private void createBmpByColors() {
        int width = 300, height = 200;
        int[] colors = initColors(width, height);
        Bitmap bmp = Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888);

        ImageView iv = (ImageView) findViewById(R.id.bmp_img);
        iv.setImageBitmap(bmp);
    }

    private int[] initColors(int width, int height) {
        int[] colors = new int[width * height];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int r = x * 255 / (width - 1);
                int g = y * 255 / (width - 1);
                int b = 255 - Math.min(r, g);
                int a = Math.max(r, g);
                colors[y * width + x] = Color.argb(a, r, g, b);
            }
        }
        return colors;
    }
     //缩放bitmap
    private void createScaledBitmap() {
        try {
            Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.scenery);
            Bitmap bitmap = Bitmap.createScaledBitmap(src, 300, 200, true);

            ImageView iv = (ImageView) findViewById(R.id.bmp_img);
            iv.setImageBitmap(bitmap);
        }catch (OutOfMemoryError error){
            error.printStackTrace();
        }
    }
}
package com.loaderman.customviewdemo;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.View;

public class LinearGradientView extends View {
    private Bitmap mDestBmp;
    private Paint mPaint;
    public LinearGradientView(Context context) {
        super(context);
        init();
    }

    public LinearGradientView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LinearGradientView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init(){
        mPaint = new Paint();

        int width = 500;
        int height = 300;
        mDestBmp = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(mDestBmp);
        Paint paint = new Paint();
        LinearGradient linearGradient = new LinearGradient(width/2,0,width/2,height,0xffffffff,0x00ffffff, Shader.TileMode.CLAMP);
        paint.setShader(linearGradient);
        canvas.drawRect(0,0,width,height,paint);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawBitmap(mDestBmp,0,0,mPaint);

        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(5);
        canvas.drawRect(0,0,mDestBmp.getWidth(),mDestBmp.getHeight(),mPaint);
    }
}
BmpBlankGradientActivity布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.loaderman.customviewdemo.BmpBlankGradientActivity">
    <com.loaderman.customviewdemo.LinearGradientView
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

 

posted on 2019-01-07 09:45  LoaderMan  阅读(706)  评论(0编辑  收藏  举报

导航