[Android]倒影效果的ImagView

來源:互聯網
上載者:User

本文源自:http://blog.csdn.net/kenshintang1215/article/details/5804236


如下:

代碼如下:
package lab.sodino.reflection;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.BitmapFactory;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.util.AttributeSet;import android.widget.ImageView;public class ReflectionImage extends ImageView {// 是否為Reflection模式private boolean mReflectionMode = true;public ReflectionImage(Context context) {super(context);}public ReflectionImage(Context context, AttributeSet attrs) {super(context, attrs);measure(0, 0);// 取得原始圖片的bitmap並重畫Bitmap originalImage = ((BitmapDrawable) this.getDrawable()).getBitmap();DoReflection(originalImage);}public ReflectionImage(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);Bitmap originalImage = ((BitmapDrawable) this.getDrawable()).getBitmap();DoReflection(originalImage);}public void setReflectionMode(boolean isRef) {mReflectionMode = isRef;}public boolean getReflectionMode() {return mReflectionMode;}// 偷懶了,只重寫了setImageResource,和建構函式裡面幹了同樣的事情@Overridepublic void setImageResource(int resId) {Bitmap originalImage = BitmapFactory.decodeResource(getResources(), resId);DoReflection(originalImage);// super.setImageResource(resId);}private void DoReflection(Bitmap originalImage) {// 原始圖片和反射圖片中間的間距final int reflectionGap = 4;int width = originalImage.getWidth();int height = originalImage.getHeight();// 反轉Matrix matrix = new Matrix();// 第一個參數為1表示x方向上以原比例為準保持不變,正數表示方向不變。// 第二個參數為-1表示y方向上以原比例為準保持不變,負數表示方向取反。matrix.preScale(1, -0.75f);// reflectionImage就是下面透明的那部分,可以設定它的高度為原始的3/4,這樣效果會更好些Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, 0, width, height * 3 / 4, matrix, false);// 建立一個新的bitmap,高度為原來的兩倍Bitmap bitmap4Reflection = Bitmap.createBitmap(width, (height + height * 3 / 4), Config.ARGB_8888);// 其寬*高 = width * (height + height * 3 / 4)Canvas canvasRef = new Canvas(bitmap4Reflection);// 先畫原始的圖片canvasRef.drawBitmap(originalImage, 0, 0, null);// 畫間距Paint deafaultPaint = new Paint();// defaultPaint不能為null,否則會有null 指標異常。canvasRef.drawRect(0, height, width, height + reflectionGap, deafaultPaint);// 畫被反轉以後的圖片canvasRef.drawBitmap(reflectionImage, 0, height + reflectionGap, null);// 建立一個漸層的蒙版放在下面被反轉的圖片上面Paint paint = new Paint();LinearGradient shader = new LinearGradient(200, originalImage.getHeight(), 10, bitmap4Reflection.getHeight()+ reflectionGap, 0x80ffffff, 0x00ffffff, TileMode.CLAMP);paint.setShader(shader);// Set the Transfer mode to be porter duff and destination inpaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));// 將蒙板畫上canvasRef.drawRect(0, height, width, bitmap4Reflection.getHeight() + reflectionGap, paint);// 調用ImageView中的setImageBitmapthis.setImageBitmap(bitmap4Reflection);}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.