欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

canvas drawBitmap demo

程序员文章站 2024-03-20 20:35:46
...

介绍

简单对图片的放大、缩小、左倾、右倾做出了demo

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button mBtn_reset,mBtn_left,mBtn_right,mBtnzoomin,mBtnzoomout;
    private MyView myView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindViews();//init button so on
    }

    private void bindViews(){
        mBtn_reset = (Button) findViewById(R.id.btn_reset);
        mBtn_left = (Button) findViewById(R.id.btn_left);
        mBtn_right = (Button) findViewById(R.id.btn_right);
        mBtnzoomin = (Button) findViewById(R.id.btn_zoomin);
        mBtnzoomout = (Button) findViewById(R.id.btn_zoomout);

        myView = (MyView) findViewById(R.id.myView);
//        myView = new MyView(this);

        mBtn_reset.setOnClickListener(this);
        mBtn_left.setOnClickListener(this);
        mBtn_right.setOnClickListener(this);
        mBtnzoomin.setOnClickListener(this);
        mBtnzoomout.setOnClickListener(this);

    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_reset:
                myView.setMethod(0);
                break;
            case R.id.btn_left:
                myView.setMethod(1);
                break;
            case R.id.btn_right:
                myView.setMethod(2);
                break;
            case R.id.btn_zoomin:
                myView.setMethod(3);
                break;
            case R.id.btn_zoomout:
                myView.setMethod(4);
                break;
            default:
                Log.i(this.getClass().getSimpleName(),"switch default");

        }
    }
}

MyView

public class MyView extends View {
    private final String TAG = "MyView";
    private Bitmap mBitmap;
    private Matrix matrix =  new Matrix();
    private float sx = 0.0f;//倾斜度
    private int mWidth,mHeight;//位宽高
    private float mScale = 1.0f;//缩放比例
    private int method = 0;

    public MyView(Context context) {
        this(context, null);
    }

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

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init() {
        Log.e(TAG, "init");
        mBitmap = BitmapFactory.decodeResource(getResources(), R.raw.img_meizi);
        mWidth = mBitmap.getWidth();
        mHeight = mBitmap.getHeight();
    }

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

        switch(method){
            case 0:
                matrix.reset();
                resetValue();
                break;
            case 1:
                sx += 0.1;
                matrix.setSkew(sx,0);
                break;
            case 2:
                sx -= 0.1;
                matrix.setSkew(sx,0);
                break;
            case 3:
                if(mScale < 2.0){
                    mScale += 0.1;
                }
                matrix.setScale(mScale,mScale);
                break;
            case 4:
                if(mScale > 0.5){
                    mScale -= 0.1;
                }
                matrix.setScale(mScale,mScale);
                break;
            default:
                Log.i(this.getClass().getSimpleName(),"switch default");
        }
        //根据原始位图创新新图片
        Bitmap bitmap = Bitmap.createBitmap(mBitmap,0,0, mWidth, mHeight, matrix,true);
        canvas.drawBitmap(bitmap, matrix,null);//绘制新图
    }

    private void resetValue() {
        mScale = 1.0f;
        sx = 0.0f;
    }

    public void setMethod(int i){
        method = i;
        postInvalidate();
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/ly_bar"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:layout_alignParentBottom="true">
        <Button
            android:id="@+id/btn_reset"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="重置" />

        <Button
            android:id="@+id/btn_left"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="左倾" />

        <Button
            android:id="@+id/btn_right"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="右倾" />

        <Button
            android:id="@+id/btn_zoomin"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="放大" />

        <Button
            android:id="@+id/btn_zoomout"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="缩小" />
    </LinearLayout>

        <com.kang.test.view.MyView
            android:id="@+id/myView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/ly_bar"/>

</RelativeLayout>
相关标签: canvas