界面布局文件:fragment_home.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <FrameLayout android:id="@+id/frame_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="@dimen/tv_marginleft" android:layout_marginTop="@dimen/tv_margintop" android:clickable="true" android:focusable="true" android:focusableInTouchMode="true" > <ImageView android:id="@+id/imageview_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/tv" android:src="@drawable/icon_tv" /> <TextView android:id="@+id/textview_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/hometext_tv_marginleft" android:layout_marginTop="@dimen/hometext_tv_margintop" android:text="@string/tv" android:textColor="@color/white" android:textSize="@dimen/home_text_size" /> </FrameLayout> <FrameLayout android:id="@+id/frame_edu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/frame_tv" android:layout_marginLeft="@dimen/edu_marginleft" android:layout_toRightOf="@+id/frame_tv" android:clickable="true" android:focusable="true" android:focusableInTouchMode="true" > <ImageView android:id="@+id/imageview_edu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/edu" android:src="@drawable/icon_edu" /> <TextView android:id="@+id/textview_edu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/hometext_edu_marginleft" android:layout_marginTop="@dimen/hometext_edu_margintop" android:text="@string/edu" android:textColor="@color/white" android:textSize="@dimen/home_text_size" /> </FrameLayout> <FrameLayout android:id="@+id/frame_movie" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/frame_edu" android:layout_marginLeft="@dimen/movie_marginleft" android:layout_toRightOf="@+id/frame_edu" android:clickable="true" android:focusable="true" android:focusableInTouchMode="true" > <ImageView android:id="@+id/imageview_movie" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/movie" android:src="@drawable/icon_movie" /> <TextView android:id="@+id/textview_movie" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/hometext_movie_marginleft" android:layout_marginTop="@dimen/hometext_movie_margintop" android:text="@string/movie" android:textColor="@color/white" android:textSize="@dimen/home_text_size" /> </FrameLayout> <FrameLayout android:id="@+id/frame_update" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/frame_movie" android:layout_marginLeft="@dimen/update_marginleft" android:layout_toRightOf="@+id/frame_movie" android:clickable="true" android:focusable="true" android:focusableInTouchMode="true" > <ImageView android:id="@+id/imageview_update" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/update" android:src="@drawable/icon_update" /> <TextView android:id="@+id/textview_update" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/hometext_update_marginleft" android:layout_marginTop="@dimen/hometext_update_margintop" android:text="@string/update" android:textColor="@color/white" android:textSize="@dimen/home_text_size" /> </FrameLayout> <FrameLayout android:id="@+id/frame_game" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/frame_edu" android:layout_marginTop="@dimen/frame_amuse_margintop" android:layout_toLeftOf="@+id/frame_movie" android:clickable="true" android:focusable="true" android:focusableInTouchMode="true" > <ImageView android:id="@+id/imageview_game" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/game" android:src="@drawable/icon_game" /> <TextView android:id="@+id/textview_game" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/hometext_game_marginleft" android:layout_marginTop="@dimen/hometext_game_margintop" android:text="@string/game" android:textColor="@color/white" android:textSize="@dimen/home_text_size" /> </FrameLayout> <FrameLayout android:id="@+id/frame_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/frame_game" android:layout_toLeftOf="@+id/frame_update" android:clickable="true" android:focusable="true" android:focusableInTouchMode="true" > <ImageView android:id="@+id/imageview_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/search" android:src="@drawable/icon_search" /> <TextView android:id="@+id/textview_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/hometext_search_marginleft" android:layout_marginTop="@dimen/hometext_search_margintop" android:text="@string/search" android:textColor="@color/white" android:textSize="@dimen/home_text_size" /> </FrameLayout> <FrameLayout android:id="@+id/frame_about" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/frame_update" android:layout_alignTop="@+id/frame_search" android:clickable="true" android:focusable="true" android:focusableInTouchMode="true" > <ImageView android:id="@+id/imageview_about" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/about" android:src="@drawable/icon_about" /> <TextView android:id="@+id/textview_about" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/hometext_about_marginleft" android:layout_marginTop="@dimen/hometext_about_margintop" android:text="@string/about" android:textColor="@color/white" android:textSize="@dimen/home_text_size" /> </FrameLayout> <ImageView android:id="@+id/reflect_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/frame_tv" android:layout_below="@+id/frame_tv" android:layout_marginTop="@dimen/reflect_tv_margintop" android:contentDescription="@string/reflect_tv" /> <ImageView android:id="@+id/reflect_game" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/frame_game" android:layout_below="@+id/frame_game" android:layout_marginTop="@dimen/reflect_game_margintop" android:contentDescription="@string/reflect_game" /> <ImageView android:id="@+id/reflect_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/frame_search" android:layout_below="@+id/frame_search" android:layout_marginTop="@dimen/reflect_search_margintop" android:contentDescription="@string/reflect_search" /> <ImageView android:id="@+id/reflect_about" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/frame_about" android:layout_below="@+id/frame_about" android:layout_marginTop="@dimen/reflect_about_margintop" android:contentDescription="@string/reflect_about" /> </RelativeLayout>
package com.gotech.launcher.home; 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.view.View; import android.view.View.MeasureSpec; import android.widget.ImageView; public class HomeReflectView { /** * Realize the reflection effect */ private static int mReflectImageHeight = 134; /** * View interface into bitmap * * @param view * @return */ public static Bitmap convertViewToBitmap(View view) { view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache(); return bitmap; } /** * Set the bitmap reflection * * @param bitmap * @return */ public static Bitmap createReflectedImage(Bitmap bitmap, int reflectHeight) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); if (height <= reflectHeight) { return null; } Matrix matrix = new Matrix(); matrix.preScale(1, -1); Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height - reflectHeight, width, reflectHeight, matrix, true); Bitmap bitmapWithReflection = Bitmap.createBitmap(width, reflectHeight, Config.ARGB_8888); Canvas canvas = new Canvas(bitmapWithReflection); canvas.drawBitmap(reflectionImage, 0, 0, null); LinearGradient shader = new LinearGradient(0, 0, 0, bitmapWithReflection.getHeight(), 0x80ffffff, 0x00ffffff, TileMode.CLAMP); Paint paint = new Paint(); paint.setShader(shader); paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); canvas.drawRect(0, 0, width, bitmapWithReflection.getHeight(), paint); return bitmapWithReflection; } public static Bitmap createCutReflectedImage(Bitmap bitmap, int cutHeight) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); int totleHeight = mReflectImageHeight + cutHeight; if (height <= totleHeight) { return null; } Matrix matrix = new Matrix(); matrix.preScale(1, -1); Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height - mReflectImageHeight - cutHeight, width, mReflectImageHeight, matrix, true); Bitmap bitmapWithReflection = Bitmap.createBitmap(width, mReflectImageHeight, Config.ARGB_8888); Canvas canvas = new Canvas(bitmapWithReflection); canvas.drawBitmap(reflectionImage, 0, 0, null); LinearGradient shader = new LinearGradient(0, 0, 0, bitmapWithReflection.getHeight(), 0x80ffffff, 0x00ffffff, TileMode.CLAMP); Paint paint = new Paint(); paint.setShader(shader); paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); canvas.drawRect(0, 0, width, bitmapWithReflection.getHeight(), paint); if (!reflectionImage.isRecycled()) { reflectionImage.recycle(); } System.gc(); return bitmapWithReflection; } public static void reflectImage(ImageView imageView, View view) { Bitmap bitmap = createCutReflectedImage(convertViewToBitmap(view), 0); imageView.setImageBitmap(bitmap); } }
private int[] mImageViewId = { R.id.reflect_tv, R.id.reflect_game, R.id.reflect_search, R.id.reflect_about }; for (int i = 0; i < mImageViewId.length; i++) { ImageView[] mView = new ImageView[mImageViewId.length]; View[] mViewId = new View[mRefFrameId.length]; mView[i] = (ImageView) view.findViewById(mImageViewId[i]); mViewId[i] = view.findViewById(mRefFrameId[i]); HomeReflectView.reflectImage(mView[i], mViewId[i]); }