博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 弹出框Dialog并缩放图片
阅读量:6071 次
发布时间:2019-06-20

本文共 5877 字,大约阅读时间需要 19 分钟。

java代码

Activity:

// 调用dialog,参数:1:自身的activity,2:Bitmap bm读取好的图片MyDialog dialog = new MyDialog(MyActivity.this, bm);dialog.show();

 Dialog类

package wdw.action.demo;import wdw.action.R;import android.app.Dialog;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Color;import android.graphics.Matrix;import android.graphics.drawable.BitmapDrawable;import android.os.Bundle;import android.util.DisplayMetrics;import android.view.GestureDetector;import android.view.GestureDetector.OnGestureListener;import android.view.MotionEvent;import android.view.View;import android.widget.Button;import android.widget.ImageView;public class MyDialog extends Dialog{private GestureDetector mGestureDetector;private Bitmap bm, bmTemp; private static final float SMALL_SCALE = 0.8f;private static final float BIG_SCALE = 1.25f;private int startX = 0, startY = 0;private int imageWidth, imageHeight;private float scaleWidth = 1, scaleHeight = 1;private int displayWidth, displayHeight;private ImageView imageView;        private Button imageSmall, imageBig, imageClose;        public MyDialog(Context context, Bitmap bm) {           super(context,R.style.dialog);           this.bm = bm;           this.bmTemp = bm;           this.mGestureDetector = new GestureDetector(context, new ViewGestureListener());}protected void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           setContentView(R.layout.img);           imageView = (ImageView) findViewById(R.id.myImageView);           imageSmall = (Button) findViewById(R.id.image_small);           imageBig = (Button) findViewById(R.id.image_big);           imageClose = (Button) findViewById(R.id.image_close);                   init();           writeImage();           bindListener();}private void init() {           DisplayMetrics dm = new DisplayMetrics();           getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);           displayWidth = dm.widthPixels;           displayHeight = dm.heightPixels;           imageWidth = bm.getWidth();           imageHeight = bm.getHeight();}private void writeImage() {           int w = (w = bmTemp.getWidth()) > displayWidth ? displayWidth : w;           int h = (h = bmTemp.getHeight()) > displayHeight ? displayHeight : h;           if (startX + w <= bmTemp.getWidth() && startY + h <= bmTemp.getHeight()) {               Bitmap bmOrg = Bitmap.createBitmap(bmTemp, startX, startY, w, h);               imageView.setImageDrawable(new BitmapDrawable(bmOrg));           } }private void buttonShow() {           imageSmall.setVisibility(View.VISIBLE);           imageBig.setVisibility(View.VISIBLE);           imageClose.setVisibility(View.VISIBLE);}private void bindListener() {           Button.OnClickListener imageButtonListener = new Button.OnClickListener() {               public void onClick(View v) {                   switch (v.getId()) {                       case R.id.image_small:                           resize(SMALL_SCALE);                           break;                       case R.id.image_big:                           resize(BIG_SCALE);                           break;                       case R.id.image_close:                           MyDialog.this.dismiss();                           break;                       default:                           break;                   }               }           };           imageSmall.setOnClickListener(imageButtonListener);           imageBig.setOnClickListener(imageButtonListener);           imageClose.setOnClickListener(imageButtonListener);}public boolean onTouchEvent(MotionEvent event) {           buttonShow();           return mGestureDetector.onTouchEvent(event);}private void resize(float scale) {       Matrix matrix = new Matrix();       scaleWidth = scaleWidth * scale;       scaleHeight = scaleHeight * scale;       matrix.postScale(scaleWidth, scaleHeight);              bmTemp = Bitmap.createBitmap(bm, 0, 0, imageWidth, imageHeight, matrix, true);       writeImage();              // 小于图片实际大小就不让缩小       if (scaleWidth * scale * imageWidth < imageWidth || scaleHeight * scale * imageHeight < imageHeight) {            imageSmall.setEnabled(false);            imageSmall.setTextColor(Color.GRAY);       } else {            imageSmall.setEnabled(true);            imageSmall.setTextColor(Color.MAGENTA);       }       // 超过屏幕大小的5倍就不让放大       if (scaleWidth * scale * imageWidth > displayWidth * 5 || scaleHeight * scale * imageHeight > displayHeight * 5) {            imageBig.setEnabled(false);            imageBig.setTextColor(Color.GRAY);       } else {            imageBig.setEnabled(true);            imageBig.setTextColor(Color.MAGENTA);       }          }class ViewGestureListener implements OnGestureListener{           public boolean onDown(MotionEvent e) {               return false;           }           public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {               return false;           }           public void onLongPress(MotionEvent e) {           }           public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {               startX = (startX + distanceX + displayWidth) > bmTemp.getWidth() ? startX : (int) (startX + distanceX);               startY = (startY + distanceY + displayHeight) > bmTemp.getHeight() ? startY : (int) (startY + distanceY);               startX = startX <= 0 ? 0 : startX;               startY = startY <= 0 ? 0 : startY;               writeImage();               return false;           }           public void onShowPress(MotionEvent e) {           }           public boolean onSingleTapUp(MotionEvent e) {               return false;           }}}

xml文件

/res/values/dialog.xml:

 /res/layout/img.xml:

  
  

 

转载地址:http://prngx.baihongyu.com/

你可能感兴趣的文章
我的友情链接
查看>>
ansible学习记录
查看>>
网思科技校园网计费解决方案
查看>>
我的友情链接
查看>>
携程 Apollo分布式部署
查看>>
2017 Hackatari Codeathon B. 2Trees(深搜)(想法)
查看>>
单词统计
查看>>
输入一个数字计算圆的面积
查看>>
在Delphi中隐藏程序进程
查看>>
AngularJS PhoneCat代码分析
查看>>
maven错误解决:编码GBK的不可映射字符
查看>>
2016/4/19 反射
查看>>
SharePoint Wiki发布页面的“保存冲突”
查看>>
oracle 10g 数据库与客户端冲突导致实例创建无监听问题
查看>>
Delphi中读取文本文件的方法(实例一)
查看>>
Linux常用命令
查看>>
Android开源代码解读の使用TelephonyManager获取移动网络信息
查看>>
想说一点东西。。。。
查看>>
css知多少(8)——float上篇
查看>>
NLB网路负载均衡管理器详解
查看>>