Android放大缩小显示图片
2013-04-23 21:37
363 查看
效果图:
Activity 代码:
activity_main.xml代码
参考 http://blog.sina.com.cn/s/blog_6ffbcfdb0100qoog.html
Activity 代码:
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.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Toast; public class MainActivity extends Activity { private ImageView photoView; private Button smallButton,bigButton,turnButton; private LinearLayout photoLayout; private int displayWidth, displayHeight; private float scaleWidth = 1, scaleHeight = 1; private int degree = 30; private Bitmap bmp; private int id=0; private void showPhoto() { photoView.setImageBitmap(bmp); //BufferedInputStream bis; //bis = new BufferedInputStream(getAssets().open(studentId + ".jpg")); //bmp = BitmapFactory.decodeStream(bis); bmp = BitmapFactory.decodeResource(this.getResources(),R.drawable.ic_launcher);//设置默认照片 photoView.setImageBitmap(bmp); } private void findViews() { photoView = (ImageView) findViewById(R.id.photoView); photoLayout = (LinearLayout) findViewById(R.id.photoLayout); smallButton = (Button) findViewById(R.id.smallButton); bigButton = (Button) findViewById(R.id.bigButton); turnButton = (Button) findViewById(R.id.turnButton); } private OnClickListener buttonClickListener=new OnClickListener() { public void onClick(View v) { switch(v.getId()) { case R.id.smallButton:small(); break; case R.id.bigButton: big(); break; case R.id.turnButton: turn(); break; } } }; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViews(); showPhoto();//显示照片 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); displayWidth = dm.widthPixels; displayHeight = dm.heightPixels - 80; System.out.println(displayWidth + "," + displayHeight); smallButton.setOnClickListener(buttonClickListener); bigButton.setOnClickListener(buttonClickListener); turnButton.setOnClickListener(buttonClickListener); } private void small() { int bmpWidth = bmp.getWidth();// 获得Bitmap的高和宽 int bmpHeight = bmp.getHeight(); double scale = 0.8;//设置缩小比例 if(scaleWidth * scale < 0.35) { Toast.makeText(this, "再缩小还看得清吗?",Toast.LENGTH_SHORT).show(); return; } scaleWidth = (float) (scaleWidth * scale);// 计算出这次要缩小的比例 scaleHeight = (float) (scaleHeight * scale); Matrix matrix = new Matrix();// 产生resize后的Bitmap对象 matrix.postScale(scaleWidth, scaleHeight); Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,matrix, true); if (id == 0) photoLayout.removeView(photoView); else photoLayout.removeView((ImageView) findViewById(id)); id++; ImageView imageView = new ImageView(this); imageView.setId(id); imageView.setImageBitmap(resizeBmp); photoLayout.addView(imageView); setContentView(photoLayout); bigButton.setEnabled(true); } private void big() { int bmpWidth = bmp.getWidth();// 获得Bitmap的高和宽 int bmpHeight = bmp.getHeight(); double scale = 1.25;// 设置放大比例 scaleWidth = (float) (scaleWidth * scale);// 计算出这次要放大的比例 scaleHeight = (float) (scaleHeight * scale); Matrix matrix = new Matrix();// 产生resize后的Bitmap对象 matrix.postScale(scaleWidth, scaleHeight); Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,matrix, true); if (id == 0) photoLayout.removeView(photoView); else photoLayout.removeView((ImageView) findViewById(id)); id++; ImageView imageView = new ImageView(this); imageView.setId(id); imageView.setImageBitmap(resizeBmp); photoLayout.addView(imageView); setContentView(photoLayout); if (scaleWidth * scale * bmpWidth > displayWidth|| scaleHeight * scale * scaleHeight > displayHeight)//大到一定程度不能再放大 { Toast.makeText(this, "已经不能再放大!",Toast.LENGTH_SHORT).show(); bigButton.setEnabled(false); } } private void turn()// 位图的旋转 { int bmpWidth = bmp.getWidth();// 获得Bitmap的高和宽 int bmpHeight = bmp.getHeight(); Matrix matrix = new Matrix();// 产生resize后的Bitmap对象 matrix.postRotate(degree); degree += 30; Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,matrix, true); if (id == 0) photoLayout.removeView(photoView); else photoLayout.removeView((ImageView) findViewById(id)); id++; ImageView imageView = new ImageView(this); imageView.setId(id); imageView.setImageBitmap(resizeBmp); photoLayout.addView(imageView); setContentView(photoLayout); } }
activity_main.xml代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/photoLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" > <LinearLayout android:paddingLeft="40sp" android:paddingRight="40sp" android:paddingTop="30sp" android:paddingBottom="20sp" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <Button android:id="@+id/smallButton" android:layout_width="80dp" android:layout_height="40dp" android:text="缩小" android:textSize="18sp"/> <Button android:id="@+id/bigButton" android:layout_width="80dp" android:layout_height="40dp" android:text="放大" android:textSize="18sp"/> <Button android:id="@+id/turnButton" android:layout_width="80dp" android:layout_height="40dp" android:text="旋转" android:textSize="18sp"/> </LinearLayout> <TextView android:id="@+id/photoInfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingBottom="20sp" android:layout_gravity="center_horizontal" /> <ImageView android:id="@+id/photoView" android:layout_width="fill_parent" android:layout_height="wrap_content" > </ImageView> </LinearLayout>
参考 http://blog.sina.com.cn/s/blog_6ffbcfdb0100qoog.html
相关文章推荐
- android用WebView显示包含文字和图片的本地网页(支持放大缩小)
- Android之PhtoView设置图片放大缩小后,图片显示在界面上左上角的解决办法
- Android中显示图片进行放大或缩小
- Android PhtoView 图片放大缩小后,图片显示在界面上左上角的解决办法
- Android手机开发:图片的放大和缩小显示ImageView
- Android中图片放大缩小(和ViewPager配合使用流畅显示)--第三方开源--PhotoView
- Android手机开发:图片的放大和缩小显示ImageView
- Android多点触摸放大缩小图片
- Android中imageView图片放大缩小及旋转功能示例代码
- 玩转Android---UI篇---ZoomControls放大缩小图片
- Android多点触控技术,实现对图片的放大缩小平移,惯性滑动等功能
- ANDROID展示HTML图文混排--实现图片缩小并点击放大
- JS----图片显示出来后放大缩小
- android 放大缩小图片
- Android 自定义ImageView实现图片手势滑动,多点触摸放大缩小效果
- Android TouchImage图片放大,缩小,点击还原,double点击放大 源码解析
- Qt 显示图片 放大 缩小 移动
- Android 图片拖动以及放大缩小
- android图片缩小和放大Matrix
- Android实现图片放大缩小