一箫一剑平生意,负尽狂名十五年。这篇文章主要讲述Android图片左右移动相关的知识,希望能为你提供帮助。
这个博文,是在上一篇的基础上修改而成,这次是控制图片左右移动,上下移动类似就不在写了
package com.yuanlp.testimage; 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--; //每次点击,向左移动 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++; //每次点击,向左移动 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); }}
【Android图片左右移动】本文出自 “YuanGuShi” 博客,请务必保留此出处http://cm0425.blog.51cto.com/10819451/1943890
推荐阅读
- JNI内存泄露JNI ERROR (app bug): local reference table overflow (max=512)
- AndroidManifest
- 13.Android插件化入门指南
- Visual Studio Emulator for Android 折腾记
- 关于九大内置对象的补充——application
- 10多种PC最佳文件共享软件(支持在线和离线传输文件)
- 不能错过的7款最佳编剧软件合集 [免费和付费]
- 10大最佳电子表格软件列表(满足你个人和业务需求)
- 7个最佳线框软件合集(使你的设计更加出色!)