Android两种不同的方法去实现图像的放大与缩小(很有帮助)
其实不算两种不同的方法,只是一个方法用的是硬编码,而另一个用的是MVC设计模式,用的都是同一个类Matrix。第一种:硬编码方式
MainActivity.java
package com.android.yhb; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.os.Bundle; import android.util.DisplayMetrics; import android.util.Log; import android.view.View; import android.widget.AbsoluteLayout; import android.widget.Button; import android.widget.ImageView; @SuppressWarnings("deprecation") public class MainActivity extends Activity { private ImageView mImageView; private Button mButton01; private Button mButton02; private AbsoluteLayout layout1; private Bitmap bmp; private int id = 0; private int displayWidth; private int displayHeight; private float scaleWidth = 1; private float scaleHeight = 1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /* 取得屏幕分辨率的大小 */ DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); displayWidth = dm.widthPixels; /* 加载资源 */ bmp = BitmapFactory.decodeResource(getResources(), R.drawable.me); mImageView = (ImageView) findViewById(R.id.imageView); layout1 = (AbsoluteLayout) findViewById(R.id.layout); mButton01 = (Button) findViewById(R.id.button_small); mButton02 = (Button) findViewById(R.id.button_big); /* 计算出来的高度要减去Button的高度 */ displayHeight = dm.heightPixels-mButton01.getHeight(); Log.e("Tag", " " + displayHeight);//这块通过Log输出发现并没有获得Button的高度,不知道怎么回事! /* 缩小监听 */ mButton01.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { small(); } }); /* 放大监听 */ mButton02.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { big(); } }); } /* small method */ private void small() { int bmpWidth = bmp.getWidth(); int bmpHeight = bmp.getHeight(); /* 设置图片缩小比例 */ double scale = 0.8; /* 计算出缩小后的长宽 */ scaleWidth = (float) (scaleWidth * scale); scaleHeight = (float) (scaleHeight * scale); /* 产生Resize后的Bitmap对象 */ Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true); if (id == 0) { /* 如果是第一次单击缩小按钮,就删除原来默认的ImageView */ layout1.removeView(mImageView); } else { /* 不然就删除上次放大或缩小产生的ImageView */ layout1.removeView((ImageView) findViewById(id)); } /* 产生新的ImageView,放入Resize后的Bitmap对象,再放入Layout中 */ id++; ImageView imageView = new ImageView(MainActivity.this); imageView.setId(id); imageView.setImageBitmap(resizeBmp); layout1.addView(imageView); setContentView(layout1); /* 将mButton02设置成可点击的 */ mButton02.setEnabled(true); } /* big method */ private void big() { int bmpWidth = bmp.getWidth(); int bmpHeight = bmp.getHeight(); /* 放大变量 */ double scale = 1.1; /* 放大以后的宽高,一定要强制转换为float型的 */ scaleWidth = (float) (scaleWidth * scale); scaleHeight = (float) (scaleHeight * scale); /* 产生resize后的Bitmap对象 */ Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true); if (id == 0) { /* 如果是第一次按就删除原来设置的ImageView */ layout1.removeView(mImageView); } else { /* 如果不是第一次按,就删除上次放大or缩小的ImageView */ layout1.removeView((ImageView) findViewById(id)); } /* 产生新的ImageView,放入Resize后的Bitmap对象,再放入Layout中 */ id++; ImageView imageView = new ImageView(MainActivity.this); imageView.setId(id); imageView.setImageBitmap(resizeBmp); layout1.addView(imageView); setContentView(layout1); /* 如果再放大会超过屏幕大小,就把Button disable */ if (scaleWidth * scale * bmpWidth > displayWidth || scaleHeight * scale * bmpHeight > displayHeight) { Log.e("Tag", " " + scaleHeight * scale * bmpHeight); mButton02.setEnabled(false); } } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android: android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:andro > <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/big" android:layout_x="61px" android:layout_y="380px" > </Button> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/small" android:layout_x="175px" android:layout_y="380px" > </Button> <ImageView android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/me" android:layout_x="0px" android:layout_y="0px" > </ImageView> </AbsoluteLayout>
关于布局这块,那么细致的去确定位置坐标,真是有点麻烦,我是用一款专门的设计软件droiddraw去做的,咋此可以下载:

实现的效果如图1:
此图为放大到最大时的截图,放大按钮被置为disabled。
注意:由于在代码70-73行有强制类型转换,所以每次放大后的长宽高都有误差,所以不是每次放大都是精准的,同理,缩小也一样。
第二种:在XML里设计(MVC)
自定义View.java
package com.android.yhb; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.view.View; public class GameView extends View implements Runnable { private int BitmapWidth = 0; private int BitmapHeight = 0; Bitmap mBitmap = null; float Scale = 1.0f; Matrix mMatrix = new Matrix(); public GameView(Context context) { super(context); // TODO Auto-generated constructor stub mBitmap = ((BitmapDrawable) getResources().getDrawable( R.drawable.myicon)).getBitmap(); BitmapWidth = mBitmap.getWidth(); BitmapHeight = mBitmap.getHeight(); new Thread(this).start(); } public void run() { while (!Thread.currentThread().isInterrupted()) { try { Thread.sleep(100); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } postInvalidate(); } } public void onDraw(Canvas canvas) { super.onDraw(canvas); mMatrix.reset(); mMatrix.postScale(Scale, Scale); Bitmap mBitmap2 = Bitmap.createBitmap(mBitmap, 0, 0, BitmapWidth, BitmapHeight, mMatrix, true); GameView.drawImage(canvas, mBitmap2, (320 - BitmapWidth) / 2, 10); mBitmap2 = null; } public static void drawImage(Canvas canvas, Bitmap bitmap, int x, int y) { canvas.drawBitmap(bitmap, x, y, null); } }
MainActivity.Java
package com.android.yhb; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; public class MainActivity extends Activity { GameView myGameView = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /*setContentView(R.layout.main);*/ myGameView = new GameView(this); setContentView(myGameView); } public boolean onKeyDown(int keycode, KeyEvent event) { if (keycode == KeyEvent.KEYCODE_DPAD_DOWN) { if (myGameView.Scale > 0.1) { Log.e("Tag", "=====>KEYCODE_DPAD_UP"); myGameView.Scale -= 0.1; } } else if (keycode == KeyEvent.KEYCODE_DPAD_UP) { if (myGameView.Scale < 2.5) { myGameView.Scale += 0.1; } } return true; } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!--<com.android.yhb.GameView android:layout_width="wrap_content" android:layout_height="wrap_content" /> --><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
这里注意应该把按键监听语句放在MainActivity.java代码里面,不应该放在自定义View里面进行监听,这样会出错。
总结:
-
/* 加载资源 */
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.me); 或mBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.myicon)).getBitmap(); - 第二个通过键盘去放大缩小,我试着通过添加按钮区控制,但是总是出错。
- 通过对代码的比较,显然易见,MVC的代码更清楚,但是用硬编码有硬编码的好处,硬代码编程运行时可以加快调用速度,而MVC会增加内存。
- 在Android中不允许ImageView在产生后,动态修改其长度和宽度,所以为了实现图片放大缩小功能,使用的方式是当用户在单击放大或缩小的按钮时,除了将图片作放大或缩小的动作外,并将原来Layout中ImageView删除,重新产生一个新的ImageView,指定图片给它后,再放入Layout里,用户看来,就好像同一张图片再放大或缩小。即方法一的代码。
- 代码有很多不足,希望大家自己去修改,也希望高人能在此给予指点。
最后更新:2017-04-02 16:47:34