首页 > 系统 > Android > 正文

Android实现的可以调整透明度的图片查看器实例

2019-10-24 20:36:10
字体:
来源:转载
供稿:网友

这篇文章主要介绍了Android实现的可以调整透明度的图片查看器,需要的朋友可以参考下

本文以实例讲解了基于Android的可以调整透明度的图片查看器实现方法,具体如下:

main.xml部分代码如下:

 

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:layout_width="fill_parent" 
  4. android:layout_height="fill_parent" 
  5. android:orientation="vertical" > 
  6.  
  7. <LinearLayout 
  8. android:layout_width="match_parent" 
  9. android:layout_height="wrap_content" > 
  10.  
  11. <Button 
  12. android:id="@+id/button1" 
  13. android:layout_width="wrap_content" 
  14. android:layout_height="wrap_content" 
  15. android:text="增大透明度" /> 
  16.  
  17. <Button 
  18. android:id="@+id/button2" 
  19. android:layout_width="wrap_content" 
  20. android:layout_height="wrap_content" 
  21. android:text="减小透明度" /> 
  22.  
  23. <Button 
  24. android:id="@+id/button3" 
  25. android:layout_width="wrap_content" 
  26. android:layout_height="wrap_content" 
  27. android:text="下一张" /> 
  28. </LinearLayout> 
  29. <!-- 定义显示整体图片的ImageView --> 
  30.  
  31. <ImageView 
  32. android:id="@+id/imageView1" 
  33. android:layout_width="wrap_content" 
  34. android:layout_height="wrap_content" 
  35. android:background="#0000ff" 
  36. android:scaleType="fitCenter" 
  37. android:src="@drawable/shuangta" /> 
  38. <!-- 定义显示局部图片的ImageView --> 
  39.  
  40. <ImageView 
  41. android:id="@+id/imageView2" 
  42. android:layout_width="wrap_content" 
  43. android:layout_height="wrap_content" 
  44. android:layout_marginTop="10dp" 
  45. android:background="#0000ff" /> 
  46.  
  47. </LinearLayout> 

java部分代码为:

 

 
  1. package android.demo; 
  2.  
  3. import android.app.Activity; 
  4. import android.graphics.Bitmap; 
  5. import android.graphics.BitmapFactory; 
  6. import android.graphics.drawable.BitmapDrawable; 
  7. import android.os.Bundle; 
  8. import android.view.MotionEvent; 
  9. import android.view.View; 
  10. import android.view.View.OnClickListener; 
  11. import android.view.View.OnTouchListener; 
  12. import android.widget.Button; 
  13. import android.widget.ImageView; 
  14.  
  15. public class AndroidDemo5Activity extends Activity { 
  16. // 定义一个访问图片的数组 
  17. int[] images = new int[] { R.drawable.lijiang, R.drawable.qiao, 
  18. R.drawable.shuangta, R.drawable.shui, R.drawable.xiangbi, 
  19. R.drawable.ic_launcher, }; 
  20. // 定义当前显示的图片 
  21. int currentImage = 2; 
  22. // 定义图片的初始透明度 
  23. private int alpha = 255; 
  24.  
  25. @Override 
  26. protected void onCreate(Bundle savedInstanceState) { 
  27. // TODO Auto-generated method stub 
  28. super.onCreate(savedInstanceState); 
  29. setContentView(R.layout.main); 
  30. final Button plusButton = (Button) findViewById(R.id.button1); 
  31. final Button minuxButton = (Button) findViewById(R.id.button2); 
  32. final Button nextButton = (Button) findViewById(R.id.button3); 
  33.  
  34. final ImageView imageview1 = (ImageView) findViewById(R.id.imageView1); 
  35. final ImageView imageview2 = (ImageView) findViewById(R.id.imageView2); 
  36.  
  37. // 定义查看下一张图片的时间监听器 
  38. nextButton.setOnClickListener(new OnClickListener() { 
  39.  
  40. @Override 
  41. public void onClick(View v) { 
  42. if (currentImage >= 5) { 
  43. currentImage = -1; 
  44. BitmapDrawable bitmap = (BitmapDrawable) imageview1 
  45. .getDrawable(); 
  46. // 如果图片还没有回收,先强制回收图片 
  47. if (!bitmap.getBitmap().isRecycled()) { 
  48. bitmap.getBitmap().recycle(); 
  49. // 改变ImageView的图片 
  50. imageview1.setImageBitmap(BitmapFactory.decodeResource( 
  51. getResources(), images[++currentImage])); 
  52. }); 
  53.  
  54. // 定义改变图片透明度的方法 
  55. OnClickListener listener = new OnClickListener() { 
  56.  
  57. @Override 
  58. public void onClick(View v) { 
  59. if (v == plusButton) { 
  60. alpha += 20; 
  61. if (v == minuxButton) { 
  62. alpha -= 20; 
  63. if (alpha > 255) { 
  64. alpha = 255; 
  65. if (alpha <= 0) { 
  66. alpha = 0; 
  67. // 改变图片的透明度 
  68. imageview1.setAlpha(alpha); 
  69.  
  70. }; 
  71.  
  72. // 为2个按钮添加监听器 
  73. plusButton.setOnClickListener(listener); 
  74. minuxButton.setOnClickListener(listener); 
  75. imageview1.setOnTouchListener(new OnTouchListener() { 
  76.  
  77. @Override 
  78. public boolean onTouch(View arg0, MotionEvent arg1) { 
  79. // TODO Auto-generated method stub 
  80. BitmapDrawable bitmapDeaw = (BitmapDrawable) imageview1 
  81. .getDrawable(); 
  82. // 获取第一个图片显示框中的位图 
  83. Bitmap bitmap = bitmapDeaw.getBitmap(); 
  84. double scale = bitmap.getWidth(); 
  85. // 或许需要显示图片的开始点 
  86. int x = (int) (arg1.getX() * scale); 
  87. int y = (int) (arg1.getY() * scale); 
  88. if (x + 120 > bitmap.getWidth()) { 
  89. x = bitmap.getWidth() - 120; 
  90. if (y + 120 > bitmap.getHeight()) { 
  91. y = bitmap.getHeight() - 120; 
  92.  
  93. // 显示图片的指定区域 
  94. imageview2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y, 
  95. 120, 120)); 
  96. imageview2.setAlpha(alpha); 
  97. return false
  98. }); 
  99.  

运行效果图如下:

Android实现的可以调整透明度的图片查看器实例

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表