標籤:android blog http java color os
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>