使用Matrix放大或缩小图片
2012-04-13 11:36
211 查看
1.将图片ex04_22_1.png放在目录res/drawable-hdpi下
2.布局文件main.xml
3.创建一个ex04_22.java的文件
4.运行结果
2.布局文件main.xml
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android:id="@+id/layout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <ImageView android:id="@+id/myImageView" android:layout_width="200dp" android:layout_height="150dp" android:src="@drawable/ex04_22_1" android:contentDescription="@string/des" > </ImageView> <Button android:id="@+id/myButton1" android:layout_width="90dp" android:layout_height="60dp" android:text="@string/small" android:textSize="18sp" android:layout_x="200dp" android:layout_y="372dp" > </Button> <Button android:id="@+id/myButton2" android:layout_width="90dp" android:layout_height="60dp" android:text="@string/big" android:textSize="18sp" android:layout_x="90dp" android:layout_y="372dp" > </Button> </AbsoluteLayout>
3.创建一个ex04_22.java的文件
package wei.cao.test; 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.view.View; import android.view.View.OnClickListener; import android.widget.AbsoluteLayout; import android.widget.Button; import android.widget.ImageView; public class ex04_22 extends Activity{ private ImageView mImageView; private Button btn1,btn2; private AbsoluteLayout layout1; private Bitmap bmp; private int id=0; private int displayWidth,displayHeight; private float scaleWidth=1,scaleHeight=1; // private final static String filename="/data/data/ex04_22.lcs/ex04_22_2.png"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //取得屏幕分辨率 DisplayMetrics dm=new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); displayWidth=dm.widthPixels; displayHeight=dm.heightPixels-80; bmp=BitmapFactory.decodeResource(this.getResources(),R.drawable.ex04_22_1); layout1=(AbsoluteLayout)findViewById(R.id.layout1); mImageView=(ImageView)findViewById(R.id.myImageView); btn1=(Button)findViewById(R.id.myButton1); btn2=(Button)findViewById(R.id.myButton2); btn1.setOnClickListener(new OnClickListener(){ public void onClick(View v){ small(); } }); btn2.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub big(); } }); } private void small(){ //获得Bitmap的高和宽 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){ layout1.removeView(mImageView); } else{ layout1.removeView((ImageView)findViewById(id)); } id++; ImageView imageView=new ImageView(this); imageView.setId(id); imageView.setImageBitmap(resizeBmp); layout1.addView(imageView); setContentView(layout1); btn2.setEnabled(true); } private void big(){ //获得Bitmap的高和宽 int bmpWidth=bmp.getWidth(); int bmpHeight=bmp.getHeight(); //设置缩小比例 double scale=1.25; //计算出这次要缩小的比例 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){ layout1.removeView(mImageView); } else{ layout1.removeView((ImageView)findViewById(id)); } id++; ImageView imageView=new ImageView(this); imageView.setId(id); imageView.setImageBitmap(resizeBmp); layout1.addView(imageView); setContentView(layout1); if(scaleWidth*scale*bmpWidth>displayWidth||scaleHeight*scale*scaleHeight>displayHeight){ btn2.setEnabled(false); } } }
4.运行结果
相关文章推荐
- 使用Matrix放大或缩小图片
- android Matrix图片随意的放大缩小,拖动
- Android图片查看支持双击放大缩小、多点触摸(多机型测试,长期使用很稳定)
- 我的Android进阶之旅------>android Matrix图片随意的放大缩小,拖动
- 我的Android进阶之旅------>android Matrix图片随意的放大缩小,拖动
- Android学习篇章10-Matrix图片放大缩小
- Android利用Matrix实现图片的放大,缩小以及移动控制
- 图片放大缩小(和ViewPager配合使用流畅显示)--第三方开源--PhotoView
- HTML5:使用Canvas和Input range控件放大缩小图片
- android 图片随意的放大缩小拖动利用Matrix
- android Matrix图片手势放大缩小,拖动
- android 图片浏览功能 图片放大缩小 使用 photoview 双击或双指缩放的ImageView
- 使用HTML5 canvas做地图(3)图片加载平移放大缩小
- js中animate 的使用图片放大缩小效果
- 我的Android进阶之旅------>android Matrix图片随意的放大缩小,拖动
- 图片放大缩小(和ViewPager配合使用流畅显示)--第三方开源--PhotoView
- 使用HTML5 canvas做地图(3)图片加载平移放大缩小
- Android开发--Matrix(一)--实现图片的动态放大缩小
- 如何使用Ubuntu手机中的手势来放大或缩小图片
- android Matrix图片随意的放大缩小,拖动