当前位置: 代码迷 >> Android >> Android兑现图片放大缩小
  详细解决方案

Android兑现图片放大缩小

热度:104   发布时间:2016-05-01 16:29:27.0
Android实现图片放大缩小
view plaincopy to clipboardprint?
  1. package?com.min.Test_Gallery;??
  2. ??
  3. import?android.app.Activity;??
  4. import?android.graphics.Bitmap;??
  5. import?android.graphics.BitmapFactory;??
  6. import?android.graphics.Color;??
  7. import?android.graphics.Matrix;??
  8. import?android.os.Bundle;??
  9. import?android.util.DisplayMetrics;??
  10. import?android.util.Log;??
  11. import?android.view.GestureDetector;??
  12. import?android.view.KeyEvent;??
  13. import?android.view.MotionEvent;??
  14. import?android.view.View;??
  15. import?android.view.GestureDetector.OnGestureListener;??
  16. import?android.view.View.OnTouchListener;??
  17. import?android.widget.Button;??
  18. import?android.widget.FrameLayout;??
  19. import?android.widget.ImageView;??
  20. import?android.widget.LinearLayout;??
  21. import?android.widget.Toast;??
  22. ??
  23. public?class?DisplayImage?extends?Activity?implements?OnTouchListener,?OnGestureListener??{??
  24. ????private?static?final?String?TAG?=?"DisplayImage";??
  25. ????private?static?final?int?FLING_MIN_DISTANCE?=?100;??
  26. ????private?static?final?int?FLING_MIN_VELOCITY?=?200;??
  27. ??????
  28. ??????
  29. ????/*?相关变量声明?*/??
  30. ????private?ImageView?mImageView;??
  31. ????private?Button?mButton01;??
  32. ????private?Button?mButton02;??
  33. ????private?FrameLayout?layout1;??
  34. ????private?LinearLayout?layoutImage;??
  35. ????private?Bitmap?bmp;??
  36. ????private?int?id=0;??
  37. ????private?int?displayWidth;??
  38. ????private?int?displayHeight;??
  39. ????private?float?scaleWidth=1;??
  40. ????private?float?scaleHeight=1;??
  41. ????private?GestureDetector?mGestureDetector;??
  42. ??????
  43. ????/**?Called?when?the?activity?is?first?created.?*/??
  44. ????@Override??
  45. ????public?void?onCreate(Bundle?savedInstanceState)????{??
  46. ????????super.onCreate(savedInstanceState);??
  47. ????????/*?加载display.xml?Layout?*/??
  48. ????????setContentView(R.layout.display);??
  49. ??????????
  50. ????????/*?取得屏幕分辨率大小?*/??
  51. ????????DisplayMetrics?dm=new?DisplayMetrics();??
  52. ????????getWindowManager().getDefaultDisplay().getMetrics(dm);??
  53. ????????displayWidth=dm.widthPixels;??
  54. ????????displayHeight=dm.heightPixels;???
  55. ??????????
  56. ????????/*?初始化相关变量?*/??
  57. ????????Bundle?bundle?=?this.getIntent().getExtras();??
  58. ????????Integer?imageId?=?bundle.getInt("imageId");??
  59. ????????Log.i(TAG,?"onCreate,?imageId?=?"?+?imageId);??
  60. ???????????????????????
  61. ????????bmp=BitmapFactory.decodeResource(getResources(),?imageId);???
  62. ????????mImageView?=?(ImageView)findViewById(R.id.myImageView);??
  63. ????????mImageView.setImageBitmap(bmp);??
  64. ????????mImageView.setOnTouchListener(this);??
  65. ????????mImageView.setLongClickable(true);??
  66. ??????????
  67. ????????layout1?=?(FrameLayout)findViewById(R.id.layout1);??
  68. ????????layoutImage?=?(LinearLayout)findViewById(R.id.layoutImage);??
  69. ????????mButton01?=?(Button)findViewById(R.id.myButton1);??
  70. ????????mButton02?=?(Button)findViewById(R.id.myButton2);???
  71. ??????????
  72. ????????/*?缩小按钮onClickListener?*/??
  73. ????????mButton01.setOnClickListener(new?Button.OnClickListener()?{??
  74. ????????????@Override??
  75. ????????????public?void?onClick(View?v)?{??
  76. ????????????????small();???
  77. ????????????}??
  78. ????????});??
  79. ??????????
  80. ????????/*?放大按钮onClickListener?*/??
  81. ????????mButton02.setOnClickListener(new?Button.OnClickListener()?{??
  82. ????????????@Override?????????
  83. ????????????public?void?onClick(View?v)?{??
  84. ????????????????big();??
  85. ????????????}???
  86. ????????});??
  87. ????}????
  88. ??????
  89. ????//?用户轻触触摸屏,由1个MotionEvent?ACTION_DOWN触发??
  90. ????@Override??
  91. ????public?boolean?onDown(MotionEvent?e)?{??
  92. ????????//?TODO?Auto-generated?method?stub??
  93. //??????Toast.makeText(this,?"onDown",?Toast.LENGTH_SHORT).show();??????
  94. ????????Log.i(TAG,?"onDown...");??
  95. ??????????
  96. ????????return?false;??
  97. ????}??
  98. ??
  99. ????/*?用户按下触摸屏、快速移动后松开,由1个MotionEvent?ACTION_DOWN,??
  100. ?????*?多个ACTION_MOVE,?1个ACTION_UP触发?
  101. ?????*?参数解释:??
  102. ?????*?e1:第1个ACTION_DOWN?MotionEvent??
  103. ?????*?e2:最后一个ACTION_MOVE?MotionEvent??
  104. ?????*?velocityX:X轴上的移动速度,像素/秒??
  105. ?????*?velocityY:Y轴上的移动速度,像素/秒??
  106. ?????*?触发条件?:??
  107. ?????*?X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒?
  108. [email protected]$OnGestureListener#onFling(android.view.MotionEvent,?android.view.MotionEvent,?float,?float)?
  109. ?????*/??
  110. ????@Override??
  111. ????public?boolean?onFling(MotionEvent?e1,?MotionEvent?e2,?float?velocityX,??
  112. ????????????float?velocityY)?{??
  113. ????????//?TODO?Auto-generated?method?stub??
  114. ????????Log.i(TAG,?"onFling...");??
  115. ??????????
  116. ????????if?(e1.getX()?-?e2.getX()?>?FLING_MIN_DISTANCE??????
  117. ?????????????????&&?Math.abs(velocityX)?>?FLING_MIN_VELOCITY)?{??????
  118. ????????????//?Fling?left???
  119. ??
  120. ?????????????Toast.makeText(this,?"Fling?Left",?Toast.LENGTH_SHORT).show();??????
  121. ?????????}?else?if?(e2.getX()?-?e1.getX()?>?FLING_MIN_DISTANCE??????
  122. ?????????????????&&?Math.abs(velocityX)?>?FLING_MIN_VELOCITY)?{??????
  123. ??
  124. ?????????????//?Fling?right???
  125. ??
  126. ?????????????Toast.makeText(this,?"Fling?Right",?Toast.LENGTH_SHORT).show();??????
  127. ?????????}????
  128. ??????????
  129. ????????return?false;??
  130. ????}??
  131. ??
  132. ????//?用户长按触摸屏,由多个MotionEvent?ACTION_DOWN触发???
  133. ????@Override??
  134. ????public?void?onLongPress(MotionEvent?e)?{??
  135. ????????//?TODO?Auto-generated?method?stub??
  136. ????????Log.i(TAG,?"onLongPress...");??
  137. ??????????
  138. ????}??
  139. ??
  140. ????//?用户按下触摸屏,并拖动,由1个MotionEvent?ACTION_DOWN,?多个ACTION_MOVE触发??
  141. ????@Override??
  142. ????public?boolean?onScroll(MotionEvent?e1,?MotionEvent?e2,?float?distanceX,??
  143. ????????????float?distanceY)?{??
  144. ????????//?TODO?Auto-generated?method?stub??
  145. ????????Log.i(TAG,?"onScroll...");??
  146. ??????????
  147. ????????return?false;??
  148. ????}??
  149. ??
  150. ????//?用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent?ACTION_DOWN触发??
  151. ????//?注意和onDown()的区别,强调的是没有松开或者拖动的状态??
  152. ????@Override??
  153. ????public?void?onShowPress(MotionEvent?e)?{??
  154. ????????//?TODO?Auto-generated?method?stub??
  155. ????????Log.i(TAG,?"onShowPress...");??
  156. ??????????
  157. ????}??
  158. ??
  159. ????//?用户(轻触触摸屏后)松开,由一个1个MotionEvent?ACTION_UP触发??
  160. ????@Override??
  161. ????public?boolean?onSingleTapUp(MotionEvent?e)?{??
  162. ????????//?TODO?Auto-generated?method?stub??
  163. ????????Log.i(TAG,?"onSingleTapUp...");??
  164. ??????????
  165. ????????return?false;??
  166. ????}??
  167. ??
  168. ????@Override??
  169. ????public?boolean?onTouch(View?v,?MotionEvent?event)?{??
  170. ????????//?TODO?Auto-generated?method?stub??
  171. ????????Log.i(TAG,?"onTouch...");??
  172. ??????????
  173. ????????//?Set?button?visible??
  174. ????????mButton01.setVisibility(View.VISIBLE);??
  175. ????????mButton02.setVisibility(View.VISIBLE);??
  176. ??????????
  177. ????????return??mGestureDetector.onTouchEvent(event);??????
  178. ????}??
  179. ??
  180. [email protected]??
  181. //??public?boolean?onTouchEvent(MotionEvent?event)?{??
  182. //??????//?TODO?Auto-generated?method?stub??
  183. //??????super.onTouchEvent(event);??
  184. //????????
  185. //??????Log.i(TAG,?"onTouchEvent");??
  186. //??????//?Set?button?visible??
  187. //??????mButton01.setVisibility(View.VISIBLE);??
  188. //??????mButton02.setVisibility(View.VISIBLE);??
  189. //????????
  190. //??????return?true;??
  191. //??}??
  192. ??????
  193. ????@Override??
  194. ????public?boolean?onKeyDown(int?keyCode,?KeyEvent?event)?{??
  195. ????????//?TODO?Auto-generated?method?stub??
  196. ????????super.onKeyDown(keyCode,?event);??
  197. ??????????
  198. ????????Log.i(TAG,?"onKeyDown...");??
  199. ????????//?Set?button?visible??
  200. ????????mButton01.setVisibility(View.VISIBLE);??
  201. ????????mButton02.setVisibility(View.VISIBLE);??
  202. ??????????
  203. ????????return?true;??
  204. ????}??
  205. ??
  206. ????/*?图片缩小的method?*/??
  207. ????private?void?small()????{??
  208. ????????int?bmpWidth=bmp.getWidth();???
  209. ????????int?bmpHeight=bmp.getHeight();??
  210. ??????????
  211. ????????Log.i(TAG,?"bmpWidth?=?"?+?bmpWidth?+?",?bmpHeight?=?"?+?bmpHeight);??
  212. ??????????
  213. ????????/*?设置图片缩小的比例?*/??
  214. ????????double?scale=0.8;??
  215. ????????/*?计算出这次要缩小的比例?*/???
  216. ????????scaleWidth=(float)?(scaleWidth*scale);???
  217. ????????scaleHeight=(float)?(scaleHeight*scale);???
  218. ????????/*?产生reSize后的Bitmap对象?*/??
  219. ????????Matrix?matrix?=?new?Matrix();??
  220. ????????matrix.postScale(scaleWidth,?scaleHeight);??
  221. ????????Bitmap?resizeBmp?=?Bitmap.createBitmap(bmp,0,0,bmpWidth,???
  222. ????????????????bmpHeight,matrix,true);???
  223. ??????????
  224. ????????if(id==0)??????{??
  225. ????????????/*?如果是第一次按,就删除原来默认的ImageView?*/??
  226. ????????????layoutImage.removeView(mImageView);??
  227. ????????}?else?{??
  228. ????????????/*?如果不是第一次按,就删除上次放大缩小所产生的ImageView?*/??
  229. ????????????layoutImage.removeView((ImageView)findViewById(id));??
  230. ????????}???
  231. ??????????
  232. ????????/*?产生新的ImageView,放入reSize的Bitmap对象,再放入Layout中?*/??
  233. ????????id++;??
  234. ????????ImageView?imageView?=?new?ImageView(this);??
  235. ????????imageView.setId(id);??
  236. ????????imageView.setImageBitmap(resizeBmp);??
  237. ????????layoutImage.addView(imageView);??
  238. ????????Log.i(TAG,?"imageView.getWidth()?=?"?+?imageView.getWidth()??
  239. ????????????????+?",?imageView.getHeight()?=?"?+?imageView.getHeight());??
  240. ????????setContentView(layout1);??
  241. ????????/*?因为图片放到最大时放大按钮会disable,所以在缩小时把它重设为enable?*/???
  242. ????????mButton02.setEnabled(true);??
  243. ????????mButton02.setTextColor(Color.MAGENTA);??
  244. ????}??
  245. ??????
  246. ????/*?图片放大的method?*/??
  247. ????private?void?big()?{??
  248. ????????int?bmpWidth=bmp.getWidth();??
  249. ????????int?bmpHeight=bmp.getHeight();??
  250. ??????????
  251. ????????Log.i(TAG,?"bmpWidth?=?"?+?bmpWidth?+?",?bmpHeight?=?"?+?bmpHeight);??
  252. ??????????
  253. ????????/*?设置图片放大的比例?*/??
  254. ????????double?scale=1.25;??
  255. ????????/*?计算这次要放大的比例?*/??
  256. ????????scaleWidth=(float)(scaleWidth*scale);??
  257. ????????scaleHeight=(float)(scaleHeight*scale);??
  258. ????????/*?产生reSize后的Bitmap对象?*/??
  259. ????????Matrix?matrix?=?new?Matrix();??
  260. ????????matrix.postScale(scaleWidth,?scaleHeight);??
  261. ????????Bitmap?resizeBmp?=?Bitmap.createBitmap(bmp,0,0,bmpWidth,???
  262. ????????????????bmpHeight,matrix,true);??
  263. ??????????
  264. ????????if(id==0)?{??
  265. ????????????/*?如果是第一次按,就删除原来设置的ImageView?*/??
  266. ????????????layoutImage.removeView(mImageView);??
  267. ????????}?else?{??
  268. ????????????/*?如果不是第一次按,就删除上次放大缩小所产生的ImageView?*/???
  269. ????????????layoutImage.removeView((ImageView)findViewById(id));??
  270. ????????}??
  271. ??????????
  272. ????????/*?产生新的ImageView,放入reSize的Bitmap对象,再放入Layout中?*/??
  273. ????????id++;??
  274. ????????ImageView?imageView?=?new?ImageView(this);??
  275. ????????imageView.setId(id);??
  276. ????????imageView.setImageBitmap(resizeBmp);??
  277. ????????layoutImage.addView(imageView);??
  278. ????????setContentView(layout1);??
  279. ????????/*?如果再放大会超过屏幕大小,就把Button?disable?*/??
  280. ????????if(?scaleWidth?*?scale?*?bmpWidth?>?bmpWidth?*?3?||??
  281. ????????????scaleHeight?*?scale?*?bmpHeight?>?bmpWidth?*?3?||??
  282. ????????????scaleWidth?*?scale?*?bmpWidth?>?displayWidth?*?5?||??
  283. ????????????scaleHeight?*?scale?*?bmpHeight?>?displayHeight?*?5)?{??
  284. ????????????????mButton02.setEnabled(false);??
  285. ????????????????mButton02.setTextColor(Color.GRAY);??
  286. ????????????}?else?{??
  287. ????????????????mButton02.setEnabled(true);??
  288. ????????????????mButton02.setTextColor(Color.MAGENTA);??
  289. ????????????}??
  290. ????????}???
  291. ??????
  292. }??

display.xml文件

view plaincopy to clipboardprint?
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <FrameLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  3. ????android:orientation="vertical"??
  4. ????android:layout_width="fill_parent"??
  5. ????android:layout_height="fill_parent"??
  6. ????android:id="@+id/layout1"??
  7. ????>??
  8. ??
  9. ????<ScrollView?xmlns:android="http://schemas.android.com/apk/res/android"?????
  10. ????????android:layout_width="fill_parent"??
  11. ????????android:layout_height="wrap_content"???
  12. ????????android:layout_weight="19"??
  13. ????????android:scrollbars="vertical"??
  14. ????????android:fadingEdge="vertical">??
  15. ????<HorizontalScrollView???
  16. ????????android:layout_height="fill_parent"??
  17. ????????android:layout_width="wrap_content">??
  18. ????????<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  19. ????????????android:orientation="horizontal"??
  20. ????????????android:layout_width="fill_parent"??
  21. ????????????android:layout_height="wrap_content"??
  22. ????????????android:gravity="center"??
  23. ????????????android:id="@+id/layoutImage"??
  24. ????????????>??
  25. ????????????<ImageView??
  26. ????????????????android:id="@+id/myImageView"??
  27. ????????????????android:layout_width="fill_parent"??
  28. ????????????????android:layout_height="wrap_content"??
  29. ????????????????android:layout_weight="19"??
  30. ????????????????android:paddingTop="5dip"??
  31. ????????????????android:paddingBottom="5dip"??
  32. ????????????????/>??
  33. ????????</LinearLayout>??
  34. ????</HorizontalScrollView?>??
  35. ????</ScrollView>???
  36. ??
  37. ????<RelativeLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  38. ????????android:layout_width="fill_parent"??
  39. ????????android:layout_height="wrap_content"??
  40. ????????>??
  41. ????????<Button??
  42. ????????????android:id="@+id/myButton1"??
  43. ????????????android:layout_width="45dip"??
  44. ????????????android:layout_height="30dip"??
  45. ????????????android:layout_alignParentLeft="true"??
  46. ????????????android:gravity="left"??
  47. ????????????style="@style/my_style_button"??
  48. ????????????android:visibility="gone"??
  49. ????????????android:text="缩小"??
  50. ????????????/>??
  51. ????????<Button??
  52. ????????????android:id="@+id/myButton2"??
  53. ????????????android:layout_width="45dip"??
  54. ????????????android:layout_height="30dip"??
  55. ????????????android:layout_alignParentRight="true"??
  56. ????????????android:gravity="right"??
  57. ????????????style="@style/my_style_button"??
  58. ????????????android:visibility="gone"??
  59. ????????????android:text="放大"??
  60. ????????????/>??
  61. ????</RelativeLayout>??
  62. </FrameLayout>?
1 楼 weixuanfeng 2011-10-25  
报错啊,图片怎么没设置呢?
2 楼 yueguc 2012-02-29  
adminelco 写道
     鄙视之,千篇一律的转载。哥要鄙视的是你这孙子转载的代码没看过。人家写错了, 你也跟着错。

白痴。。。
  相关解决方案