Android 设置图片倒影效果
首先,贴出效果图:
1.布局文件main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" > <TextView android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "@string/hello" /> <ImageView android:id= "@+id/picture_qian" android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:src= "@drawable/horse" /> </LinearLayout> |
2.MainActivity界面java代码:
1 package com.easymorse.picture; 2 3 import android.app.Activity; 4 import android.graphics.Bitmap; 5 import android.graphics.Bitmap.Config; 6 import android.graphics.Canvas; 7 import android.graphics.LinearGradient; 8 import android.graphics.Matrix; 9 import android.graphics.Paint; 10 import android.graphics.PorterDuff.Mode; 11 import android.graphics.PorterDuffXfermode; 12 import android.graphics.Shader.TileMode; 13 import android.graphics.drawable.BitmapDrawable; 14 import android.os.Bundle; 15 import android.widget.ImageView; 16 17 public class MainActivity extends Activity { 18 /** Called when the activity is first created. */ 19 @Override 20 public void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.main); 23 ImageView imageView2 = (ImageView) findViewById(R.id.picture_qian); 24 25 Bitmap bmp = ((BitmapDrawable) getResources().getDrawable( 26 R.drawable.horse)).getBitmap(); 27 28 imageView2.setImageBitmap(createReflectedImage(bmp)); 29 } 30 31 public static Bitmap createReflectedImage(Bitmap originalImage) { 32 33 final int reflectionGap = 4; 34 35 int width = originalImage.getWidth(); 36 int height = originalImage.getHeight(); 37 38 Matrix matrix = new Matrix(); 39 matrix.preScale(1, -1); 40 41 Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, 42 height / 2, width, height / 2, matrix, false); 43 44 Bitmap bitmapWithReflection = Bitmap.createBitmap(width, 45 (height + height / 2), Config.ARGB_8888); 46 47 Canvas canvas = new Canvas(bitmapWithReflection); 48 49 canvas.drawBitmap(originalImage, 0, 0, null); 50 51 Paint defaultPaint = new Paint(); 52 canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint); 53 54 canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); 55 56 Paint paint = new Paint(); 57 LinearGradient shader = new LinearGradient(0, 58 originalImage.getHeight(), 0, bitmapWithReflection.getHeight() 59 + reflectionGap, 0x70ffffff, 0x00ffffff, 60 TileMode.MIRROR); 61 62 paint.setShader(shader); 63 64 paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); 65 66 canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() 67 + reflectionGap, paint); 68 69 return bitmapWithReflection; 70 } 71 }
分类:
Android 实例开发学习
标签:
android图片倒影
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
· JDK 24 发布,新特性解读!
· C# 中比较实用的关键字,基础高频面试题!
· .NET 10 Preview 2 增强了 Blazor 和.NET MAUI
· Ollama系列05:Ollama API 使用指南
· 为什么AI教师难以实现