zoukankan      html  css  js  c++  java
  • 创建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>
  • 相关阅读:
    Arduino uno 教程~持续更新~
    Arduino uno LED灯实验
    Arduino uno 引脚说明
    面包板的使用
    数量经济学推荐的Julia教程
    已知一点经纬度和距离,方位角;求另外一点的经纬度
    a recipe kindly provided by Dimas for kikuchi
    发现了拯救“文献多的一团麻”的工具
    matlab中diff的用法
    matlabR2017安装
  • 原文地址:https://www.cnblogs.com/loaderman/p/10231540.html
Copyright © 2011-2022 走看看