Android中imageView图片放大缩小及旋转功能示例代码

  

  

  

  

<强> 1)设置图片放大缩小效果

  

第一步:将<代码> & lt; ImageView> android: scaleType 设置为<代码>“fitCenter”

        android: scaleType=" fitCenter "      

第二步:获取屏幕的宽度

        DisplayMetrics dm=new DisplayMetrics ();   .getDefaultDisplay getWindowManager () () .getMetrics (dm);   dm.widthPixels      

第三步:设置seekBar的最大方面值进步为屏幕宽度

        sb_one.setMax (dm.widthPixels);      

第四步:设置imageview的布局参数,也就是宽和高,也就是画布的宽高

        int宽度=进步;   int身高=进展* 3/4;   iv_pic。setLayoutParams(新LinearLayout。LayoutParams(宽度、高度));      

<强> 2)设置图片旋转方法

  

第一步:给矩阵设置角度,用于新的位图

        私人矩阵矩阵;   matrix.setRotate ((int) * 3.60(进展);      

第二步:获取位图资源

        BitmapDrawable BitmapDrawable=(BitmapDrawable) (getresource () .getDrawable (R.drawable.image1));   位图的位图=bitmapDrawable.getBitmap ();      

第三步:重建位图用于显示

        位图newBitmap=位图。createBitmap(位图,0,0,bitmap.getWidth (), bitmap.getHeight(),矩阵,false),      

第四步:给imageview设置新的位图

        iv_pic.setImageBitmap (newBitmap);      

  

效果图:

  

 Android中imageView图片放大缩小及旋转功能示例代码

  

设置大小和设置旋转的效果图

  

 Android中imageView图片放大缩小及旋转功能示例代码”> <img src=

  

代码:   

fry.Activity02         包煎;   进口com.example.iamgeViewDemo1.R;   进口android.app.Activity;   进口android.graphics.Bitmap;   进口android.graphics.Matrix;   进口android.graphics.drawable.BitmapDrawable;   进口android.os.Bundle;   进口android.util.DisplayMetrics;   进口android.view.ViewGroup.LayoutParams;   进口android.widget.ImageView;   进口android.widget.LinearLayout;   进口android.widget.SeekBar;   进口android.widget.SeekBar.OnSeekBarChangeListener;   公共类Activity02扩展活动实现>   & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android "   android: layout_width=" match_parent "   android: layout_height=" match_parent "   面向android:=按怪薄北?   & lt; ImageView   android: id=癅 + id/iv_pic”   android: layout_width=" match_parent "   android: layout_height=" 300下降”   android:背景=" @android:颜色/黑”   android: scaleType=" fitCenter "   android: src=" https://www.yisu.com/zixun/@drawable image1 "/比;   & lt; !——设置图片的显示方式:把图片按比例扩大/缩小到视图的宽度,居中显示——比;   & lt; SeekBar   android: id=癅 + id/sb_one”   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android:进步=" 100 "/比;   & lt; TextView   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android:文本="拖动来缩放图片”/比;   & lt; SeekBar   android: id=癅 + id/sb_two”   android: layout_width=" match_parent "   android: layout_height=" wrap_content "/比;   & lt; TextView   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android:文本="拖动来旋转图片”/比;   & lt;/LinearLayout>      

  

, 1,设置图像居中显示

        android: scaleType=" fitCenter "      

  

以上所述是小编给大家介绍的Android中imageView图片放大缩小及旋转功能示例代码,希望对大家有所帮助,如果大家有任何疑问请给我留的言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

Android中imageView图片放大缩小及旋转功能示例代码