1、效果图:
2、核心代码:
package com.example.pic_reflection; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.LinearGradient; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffXfermode; import android.graphics.Shader.TileMode; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView img = null; private ImageView orginalImg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.orginalImg = (ImageView) this.findViewById(R.id.img); this.orginalImg.setBackgroundResource(R.drawable.b); this.img = (ImageView) this.findViewById(R.id.reflection1); this.img.setImageBitmap(createReflectedImage(((BitmapDrawable) this.getResources().getDrawable(R.drawable.b)).getBitmap(), 110)); } public static Bitmap createReflectedImage(Bitmap originalImage, int reflectionHeight) { int width = originalImage.getWidth(); int height = originalImage.getHeight(); Matrix matrix = new Matrix(); // 实现图片翻转90度 matrix.preScale(1, -1); if (reflectionHeight > height) reflectionHeight = height; // 创建倒影图片(是原始图片的一半大小) Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height - reflectionHeight, width, reflectionHeight, matrix, false); // 创建倒影图片 Bitmap finalReflection = Bitmap.createBitmap(width, reflectionHeight, Config.ARGB_8888); // 创建画布 Canvas canvas = new Canvas(finalReflection); // canvas.drawBitmap(originalImage, 0, 0, null); // 把倒影图片画到画布上 canvas.drawBitmap(reflectionImage, 0, 0, null); Paint shaderPaint = new Paint(); // 创建线性渐变LinearGradient对象 LinearGradient shader = new LinearGradient(0, 0, 0, finalReflection.getHeight() + 1, 0x70ffffff, 0x00ffffff, TileMode.MIRROR); shaderPaint.setShader(shader); shaderPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // 画布画出反转图片大小区域,然后把渐变效果加到其中,就出现了图片的倒影效果。 canvas.drawRect(0, 0, width, finalReflection.getHeight(), shaderPaint); return finalReflection; } }
3、main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <include layout="@layout/activity_main" /> <ImageView android:id="@+id/reflection1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
4、activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:id="@+id/img" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" /> <TextView android:id="@+id/lblBanner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="#88000000" android:ellipsize="end" android:gravity="center_horizontal|center_vertical" android:singleLine="true" android:text="小汽车一枚" android:textColor="#FFF" /> </FrameLayout>
android 中实现图片倒影效果,码迷,mamicode.com
时间: 2024-10-11 21:35:42