這個博文,是在上一篇的基礎(chǔ)上修改而成,這次是控制圖片左右移動,上下移動類似就不在寫了
創(chuàng)新互聯(lián)是一家專業(yè)提供于洪企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站制作、成都網(wǎng)站設(shè)計、H5頁面制作、小程序制作等業(yè)務(wù)。10年已為于洪眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。
package com.yuanlp.testp_w_picpath; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { private ImageView iv; private int dx=0; //左移的距離 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); iv = (ImageView) findViewById(R.id.iv); } public void turnBig(View view){ Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.meinv); Matrix matrix=new Matrix(); matrix.setScale(2,2); //放大四倍,水平方向2倍,豎直方向2倍 //買一張紙 Bitmap bitmap1 = Bitmap.createBitmap(bitmap.getWidth() * 2, bitmap.getHeight() * 2, bitmap.getConfig()); //買個畫板 Canvas canvas = new Canvas(bitmap1); //臨摹會話 Paint paint = new Paint(); paint.setColor(Color.BLACK); canvas.drawBitmap(bitmap,matrix,paint); //傳入新紙 iv.setImageBitmap(bitmap1); } public void turnSmall(View view){ Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.meinv); Matrix matrix=new Matrix(); matrix.setScale(0.5f,0.5f); //買個紙 Bitmap bitmap1 = Bitmap.createBitmap(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getConfig()); //買個畫板 Canvas canvas=new Canvas(bitmap1); Paint paint=new Paint(); paint.setColor(Color.BLACK); canvas.drawBitmap(bitmap,matrix,paint); iv.setImageBitmap(bitmap1); } public void turnLeft(View view){ Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.meinv); dx--; //每次點(diǎn)擊,向左移動 Matrix matrix=new Matrix(); matrix.setTranslate(dx,0); //豎直方向不移動,只有左邊移動 //買個紙 Bitmap bitmap1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); //買個畫板 Canvas canvas=new Canvas(bitmap1); Paint paint=new Paint(); canvas.drawColor(Color.WHITE); paint.setColor(Color.BLACK); canvas.drawBitmap(bitmap,matrix,paint); iv.setImageBitmap(bitmap1); } public void turnRight(View view){ Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.meinv); dx++; //每次點(diǎn)擊,向左移動 Matrix matrix=new Matrix(); matrix.setTranslate(dx,0); //豎直方向不移動,只有左邊移動 //買個紙 Bitmap bitmap1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); //買個畫板 Canvas canvas=new Canvas(bitmap1); Paint paint=new Paint(); canvas.drawColor(Color.WHITE); paint.setColor(Color.BLACK); canvas.drawBitmap(bitmap,matrix,paint); iv.setImageBitmap(bitmap1); } }