android圖片倒影控制項ReflectionImage

來源:互聯網
上載者:User

 
ReflectionImage是從android.widget.ImageView繼承而來,使用方法和其他的Android控制項一樣 

實現思路是從某個網站上剽竊過來的。 

實現起來很簡單,詳細的解釋寫到注釋裡面,總體思路就是在Canvas裡面重新把原始圖片畫一次,反轉圖片用Matrix來做,透明處理實際上是加了一層漸層的蒙板. 

import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.LinearGradient;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.PorterDuffXfermode;import android.graphics.Bitmap.Config;import android.graphics.PorterDuff.Mode;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);//取得原始圖片的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();matrix.preScale(1, -1);  //reflectionImage就是下面透明的那部分,可以設定它的高度為原始的3/4,這樣效果會更好些Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,0, width, height, matrix, false);//建立一個新的bitmap,高度為原來的兩倍Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height), Config.ARGB_8888);Canvas canvasRef = new Canvas(bitmapWithReflection);//先畫原始的圖片canvasRef.drawBitmap(originalImage, 0, 0, null);//畫間距Paint deafaultPaint = new Paint();canvasRef.drawRect(0, height, width, height + reflectionGap, deafaultPaint);//畫被反轉以後的圖片canvasRef.drawBitmap(reflectionImage, 0, height + reflectionGap, null);// 建立一個漸層的蒙版放在下面被反轉的圖片上面Paint paint = new Paint();LinearGradient shader = new LinearGradient(0,originalImage.getHeight(), 0, bitmapWithReflection.getHeight()+ reflectionGap, 0x80ffffff, 0x00ffffff, TileMode.CLAMP);// Set the paint to use this shader (linear gradient)paint.setShader(shader);// Set the Transfer mode to be porter duff and destination inpaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));// Draw a rectangle using the paint with our linear gradientcanvasRef.drawRect(0, height, width, bitmapWithReflection.getHeight()+ reflectionGap, paint);  //調用ImageView中的setImageBitmapthis.setImageBitmap(bitmapWithReflection);}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.