...">
当前位置:   article > 正文

图片放大缩小旋转左移右移镜像倒影android

图片放大缩小旋转左移右移镜像倒影android

                             图片放大缩小旋转左移右移镜像倒影

demo:

TestImage

1. 将 meinv.jpg 图片拷贝到以下目录TestImage\app\src\main\res\mipmap-hdpi

1.AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.glsite.testimage">
  4. <application
  5. android:allowBackup="true"
  6. android:icon="@mipmap/ic_launcher"
  7. android:label="@string/app_name"
  8. android:roundIcon="@mipmap/ic_launcher_round"
  9. android:supportsRtl="true"
  10. android:theme="@style/AppTheme">
  11. <activity android:name=".MainActivity">
  12. <intent-filter>
  13. <action android:name="android.intent.action.MAIN" />
  14. <category android:name="android.intent.category.LAUNCHER" />
  15. </intent-filter>
  16. </activity>
  17. </application>
  18. </manifest>

2.activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <ImageView
  9. android:id="@+id/iv"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. app:layout_constraintBottom_toBottomOf="parent"
  13. app:layout_constraintLeft_toLeftOf="parent"
  14. app:layout_constraintRight_toRightOf="parent"
  15. app:layout_constraintTop_toTopOf="parent"
  16. android:src="@mipmap/meinv"/>
  17. <Button
  18. android:id="@+id/button"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_marginStart="8dp"
  22. android:layout_marginLeft="8dp"
  23. android:layout_marginTop="8dp"
  24. android:onClick="turnBig"
  25. android:text="放大"
  26. app:layout_constraintStart_toStartOf="parent"
  27. app:layout_constraintTop_toTopOf="parent" />
  28. <Button
  29. android:id="@+id/button2"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:layout_marginTop="8dp"
  33. android:layout_marginEnd="8dp"
  34. android:layout_marginRight="8dp"
  35. android:onClick="turnSmall"
  36. android:text="缩小"
  37. app:layout_constraintEnd_toEndOf="parent"
  38. app:layout_constraintTop_toTopOf="parent" />
  39. </android.support.constraint.ConstraintLayout>

3.activity_main2.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <ImageView
  9. android:id="@+id/iv"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. app:layout_constraintBottom_toBottomOf="parent"
  13. app:layout_constraintLeft_toLeftOf="parent"
  14. app:layout_constraintRight_toRightOf="parent"
  15. app:layout_constraintTop_toTopOf="parent"
  16. android:src="@mipmap/meinv"/>
  17. <Button
  18. android:id="@+id/button"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_marginStart="8dp"
  22. android:layout_marginLeft="8dp"
  23. android:layout_marginTop="8dp"
  24. android:onClick="turnLeft"
  25. android:text="左移"
  26. app:layout_constraintStart_toStartOf="parent"
  27. app:layout_constraintTop_toTopOf="parent" />
  28. <Button
  29. android:id="@+id/button2"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:layout_marginTop="8dp"
  33. android:layout_marginEnd="8dp"
  34. android:layout_marginRight="8dp"
  35. android:onClick="turnRight"
  36. android:text="右移"
  37. app:layout_constraintEnd_toEndOf="parent"
  38. app:layout_constraintTop_toTopOf="parent" />
  39. </android.support.constraint.ConstraintLayout>

4.activity_main3.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <ImageView
  9. android:id="@+id/iv"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. app:layout_constraintBottom_toBottomOf="parent"
  13. app:layout_constraintLeft_toLeftOf="parent"
  14. app:layout_constraintRight_toRightOf="parent"
  15. app:layout_constraintTop_toTopOf="parent"
  16. android:src="@mipmap/meinv"/>
  17. <Button
  18. android:id="@+id/button"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_marginStart="8dp"
  22. android:layout_marginLeft="8dp"
  23. android:layout_marginTop="8dp"
  24. android:onClick="turnCircleLeft"
  25. android:text="逆时针旋转"
  26. app:layout_constraintStart_toStartOf="parent"
  27. app:layout_constraintTop_toTopOf="parent" />
  28. <Button
  29. android:id="@+id/button2"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:layout_marginTop="8dp"
  33. android:layout_marginEnd="8dp"
  34. android:layout_marginRight="8dp"
  35. android:onClick="turnCircleRight"
  36. android:text="顺时针旋转"
  37. app:layout_constraintEnd_toEndOf="parent"
  38. app:layout_constraintTop_toTopOf="parent" />
  39. </android.support.constraint.ConstraintLayout>

5.activity_main4.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <ImageView
  9. android:id="@+id/iv"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. app:layout_constraintBottom_toBottomOf="parent"
  13. app:layout_constraintLeft_toLeftOf="parent"
  14. app:layout_constraintRight_toRightOf="parent"
  15. app:layout_constraintTop_toTopOf="parent"
  16. android:src="@mipmap/meinv"/>
  17. <Button
  18. android:id="@+id/button"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_marginStart="8dp"
  22. android:layout_marginLeft="8dp"
  23. android:layout_marginTop="8dp"
  24. android:onClick="click01"
  25. android:text="倒影"
  26. app:layout_constraintStart_toStartOf="parent"
  27. app:layout_constraintTop_toTopOf="parent" />
  28. <Button
  29. android:id="@+id/button2"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:layout_marginTop="8dp"
  33. android:layout_marginEnd="8dp"
  34. android:layout_marginRight="8dp"
  35. android:onClick="click02"
  36. android:text="镜面"
  37. app:layout_constraintEnd_toEndOf="parent"
  38. app:layout_constraintTop_toTopOf="parent" />
  39. </android.support.constraint.ConstraintLayout>

 

6.MainActivity.java

  1. package com.glsite.testimage;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Matrix;
  7. import android.graphics.Paint;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.widget.ImageView;
  12. public class MainActivity extends AppCompatActivity {
  13. private ImageView mIv;
  14. private int dx = 0;
  15. private int degress = 0;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main4);
  20. mIv = findViewById(R.id.iv);
  21. }
  22. /**
  23. * 放大
  24. * @param view
  25. */
  26. public void turnBig(View view) {
  27. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.meinv);
  28. Matrix matrix = new Matrix();
  29. matrix.setScale(2, 2);
  30. // 1.买一张纸
  31. Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth() * 2, bitmap.getHeight() * 2, bitmap.getConfig());
  32. // 2.买一个画板
  33. Canvas canvas = new Canvas(newBitmap);
  34. // 3.临摹绘画
  35. Paint paint = new Paint();
  36. paint.setColor(Color.BLACK);
  37. canvas.drawBitmap(bitmap, matrix, paint);
  38. mIv.setImageBitmap(newBitmap);
  39. }
  40. /**
  41. * 缩小
  42. * @param view
  43. */
  44. public void turnSmall(View view) {
  45. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.meinv);
  46. Matrix matrix = new Matrix();
  47. matrix.setScale(0.5f, 0.5f);
  48. // 1.买一张纸
  49. Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getConfig());
  50. // 2.买一个画板
  51. Canvas canvas = new Canvas(newBitmap);
  52. // 3.临摹绘画
  53. Paint paint = new Paint();
  54. paint.setColor(Color.BLACK);
  55. canvas.drawBitmap(bitmap, matrix, paint);
  56. mIv.setImageBitmap(newBitmap);
  57. }
  58. /**
  59. * 左移
  60. * @param view
  61. */
  62. public void turnLeft(View view) {
  63. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.meinv);
  64. Matrix matrix = new Matrix();
  65. dx--;
  66. matrix.setTranslate(dx, 0);
  67. // 1.买一张纸
  68. Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
  69. // 2.买一个画板
  70. Canvas canvas = new Canvas(newBitmap);
  71. // 3.临摹绘画
  72. Paint paint = new Paint();
  73. canvas.drawColor(Color.WHITE);
  74. paint.setColor(Color.BLACK);
  75. canvas.drawBitmap(bitmap, matrix, paint);
  76. mIv.setImageBitmap(newBitmap);
  77. }
  78. /**
  79. * 右移
  80. * @param view
  81. */
  82. public void turnRight(View view) {
  83. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.meinv);
  84. Matrix matrix = new Matrix();
  85. dx++;
  86. matrix.setTranslate(dx, 0);
  87. // 1.买一张纸
  88. Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
  89. // 2.买一个画板
  90. Canvas canvas = new Canvas(newBitmap);
  91. // 3.临摹绘画
  92. Paint paint = new Paint();
  93. canvas.drawColor(Color.WHITE);
  94. paint.setColor(Color.BLACK);
  95. canvas.drawBitmap(bitmap, matrix, paint);
  96. mIv.setImageBitmap(newBitmap);
  97. }
  98. /**
  99. * 逆时针旋转
  100. * @param view
  101. */
  102. public void turnCircleLeft(View view) {
  103. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.meinv);
  104. Matrix matrix = new Matrix();
  105. matrix.setRotate(--degress, bitmap.getWidth(), bitmap.getHeight());
  106. matrix.postTranslate(100, 100);
  107. // 1.买一张纸
  108. Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth()*2, bitmap.getHeight()*2, bitmap.getConfig());
  109. // 2.买一个画板
  110. Canvas canvas = new Canvas(newBitmap);
  111. // 3.临摹绘画
  112. Paint paint = new Paint();
  113. canvas.drawColor(Color.WHITE);
  114. paint.setColor(Color.BLACK);
  115. paint.setAntiAlias(true);// 消除锯齿
  116. canvas.drawBitmap(bitmap, matrix, paint);
  117. mIv.setImageBitmap(newBitmap);
  118. }
  119. /**
  120. * 顺时针旋转
  121. * @param view
  122. */
  123. public void turnCircleRight(View view) {
  124. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.meinv);
  125. Matrix matrix = new Matrix();
  126. matrix.setRotate(++degress, bitmap.getWidth(), bitmap.getHeight());
  127. matrix.postTranslate(100, 100);
  128. // 1.买一张纸
  129. Bitmap newBitmap = Bitmap.createBitmap((int)(bitmap.getWidth()*1.5), (int)(bitmap.getHeight()*1.5), bitmap.getConfig());
  130. // 2.买一个画板
  131. Canvas canvas = new Canvas(newBitmap);
  132. // 3.临摹绘画
  133. Paint paint = new Paint();
  134. canvas.drawColor(Color.WHITE);
  135. paint.setColor(Color.BLACK);
  136. paint.setAntiAlias(true);// 消除锯齿
  137. canvas.drawBitmap(bitmap, matrix, paint);
  138. mIv.setImageBitmap(newBitmap);
  139. }
  140. /**
  141. * 倒影
  142. * @param view
  143. */
  144. public void click01(View view) {
  145. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.meinv);
  146. Matrix matrix = new Matrix();
  147. matrix.setScale(1, -1);
  148. matrix.postTranslate(0, bitmap.getHeight());
  149. // 1.买一张纸
  150. Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
  151. // 2.买一个画板
  152. Canvas canvas = new Canvas(newBitmap);
  153. // 3.临摹绘画
  154. Paint paint = new Paint();
  155. canvas.drawColor(Color.WHITE);
  156. paint.setColor(Color.BLACK);
  157. paint.setAntiAlias(true);// 消除锯齿
  158. canvas.drawBitmap(bitmap, matrix, paint);
  159. mIv.setImageBitmap(newBitmap);
  160. }
  161. /**
  162. * 镜面
  163. * @param view
  164. */
  165. public void click02(View view) {
  166. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.meinv);
  167. Matrix matrix = new Matrix();
  168. matrix.setScale(-1, 1);
  169. matrix.postTranslate(bitmap.getWidth(), 0);
  170. // 1.买一张纸
  171. Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
  172. // 2.买一个画板
  173. Canvas canvas = new Canvas(newBitmap);
  174. // 3.临摹绘画
  175. Paint paint = new Paint();
  176. canvas.drawColor(Color.WHITE);
  177. paint.setColor(Color.BLACK);
  178. paint.setAntiAlias(true);// 消除锯齿
  179. canvas.drawBitmap(bitmap, matrix, paint);
  180. mIv.setImageBitmap(newBitmap);
  181. }
  182. }

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/262953
推荐阅读
相关标签
  

闽ICP备14008679号