canvas drawBitmap demo
2017-12-04 14:07
239 查看
介绍
简单对图片的放大、缩小、左倾、右倾做出了demoMainActivity
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>
相关文章推荐
- Android 使用Canvas中的drawBitmap方法绘制拉伸的图片
- android canvas onDraw和Draw的bitmap区别
- 详解 Canvas.drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
- canvas.drawBitmap(bitmap, src, dst, paint)
- android中Canvas使用drawBitmap绘制图片
- Canvas的drawBitmap以及Paint的PorterDuffXfermode使用心得
- canvas.drawBitmap(bitmap, src, dst, paint)
- android canvas.drawBitmap 方法的区别
- java.lang.RuntimeException: Canvas: trying to draw too large(203212800bytes) bitmap.
- Android canvas.drawBitmap(...)Bitmmap图片在模拟器和真机上位置不一致问题
- canvas.drawBitmap()频繁调用导致应用崩溃问题
- android中Canvas使用drawBitmap绘制图片
- Canvas drawText()和drawBitmap()
- Android Canvas使用drawBitmap绘制图片
- 自定义imageview中的canvas.drawBitmap方法
- android中Canvas使用drawBitmap绘制图片
- java.lang.RuntimeException: Canvas:trying to draw too large(......)bitmap
- canvas.drawBitmap(bitmap, src, dst, paint) 方法,个人理解,
- Android canvas drawBitmap方法详解及实例
- Canvas开篇之drawBitmap方法讲解