今天記錄一下TextView的倒影效果,顯示一串文字,而後在文字的下方顯示出它的倒影,先上效果圖:java
最重要的就是View中getDrawingCache()方法,該方法能夠獲取cache中的圖像,而後繪製出來。android
廢話很少說,我是想寫一個帶有倒影的時間,時間能夠走動。首先先寫一個帶有時間走動的View,這個很簡單,獲取當前時間,而後開啓一個線程,隔一秒獲取當前時間一次,而後顯示在TextView上,固然,咱們寫控件,就須要繼承TextView,代碼以下:canvas
1 package com.alex.reflecttextview; 2 3 import java.util.Calendar; 4 5 import android.content.Context; 6 import android.os.Handler; 7 import android.os.Message; 8 import android.text.format.DateFormat; 9 import android.util.AttributeSet; 10 import android.widget.TextView; 11 12 public class TimeView extends TextView { 13 14 private static final int MESSAGE_TIME = 1; 15 16 public TimeView(Context context, AttributeSet attrs) { 17 super(context, attrs); 18 new TimeThread().start(); 19 } 20 21 public class TimeThread extends Thread { 22 @Override 23 public void run() { 24 do { 25 try { 26 Message msg = new Message(); 27 msg.what = MESSAGE_TIME; 28 mHandler.sendMessage(msg); 29 Thread.sleep(1000); 30 } catch (InterruptedException e) { 31 e.printStackTrace(); 32 } 33 } while (true); 34 } 35 } 36 37 private Handler mHandler = new Handler() { 38 39 @Override 40 public void handleMessage(Message msg) { 41 super.handleMessage(msg); 42 switch (msg.what) { 43 case MESSAGE_TIME: 44 setTime(); 45 break; 46 47 default: 48 break; 49 } 50 } 51 }; 52 53 public void setTime() { 54 long sysTime = System.currentTimeMillis(); 55 Calendar calendar = Calendar.getInstance(); 56 calendar.setTimeInMillis(sysTime); 57 String sysTimeStr = DateFormat.format("hh:mm", sysTime).toString(); 58 if(calendar.get(Calendar.AM_PM) == 0) { 59 sysTimeStr += " AM"; 60 } else { 61 sysTimeStr += " PM"; 62 } 63 setText(sysTimeStr.replace("1", " 1")); 64 } 65 }
如今只須要在佈局文件中調用該控件就能夠實現一個走動的時間了。app
第二步就是須要給這個走動的時間加上倒影了,咱們就須要寫一個控件來繼承上面一個時間走動的控件,就能夠實現帶有倒影的時間走動的View了,下面是帶有倒影的代碼:ide
1 package com.alex.reflecttextview; 2 3 4 import android.content.Context; 5 import android.graphics.Bitmap; 6 import android.graphics.Canvas; 7 import android.graphics.LinearGradient; 8 import android.graphics.Matrix; 9 import android.graphics.Paint; 10 import android.graphics.PorterDuff.Mode; 11 import android.graphics.PorterDuffXfermode; 12 import android.graphics.Shader.TileMode; 13 import android.util.AttributeSet; 14 15 public class ReflectTextView extends TimeView { 16 17 private Matrix mMatrix; 18 private Paint mPaint; 19 20 public ReflectTextView(Context context, AttributeSet attrs) { 21 super(context, attrs); 22 init(); 23 } 24 25 private void init() { 26 mMatrix = new Matrix(); 27 mMatrix.preScale(1, -1); 28 } 29 30 @Override 31 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 32 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 33 setMeasuredDimension(getMeasuredWidth(), (int)(getMeasuredHeight()*1.67)); 34 } 35 36 @Override 37 protected void onDraw(Canvas canvas) { 38 super.onDraw(canvas); 39 int height = getHeight(); 40 int width = getWidth(); 41 setDrawingCacheEnabled(true); 42 Bitmap originalImage = Bitmap.createBitmap(getDrawingCache()); 43 Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height/5, width, height/2, mMatrix, false); 44 canvas.drawBitmap(reflectionImage, 0, height/3f, null); 45 if(mPaint == null) { 46 mPaint = new Paint(); 47 LinearGradient shader = new LinearGradient(0, height/2, 0, 48 height, 0x7fffffff, 0x0fffffff, TileMode.CLAMP); 49 mPaint.setShader(shader); 50 mPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); 51 } 52 canvas.drawRect(0, height/2f, width, height, mPaint); 53 } 54 55 @Override 56 protected void onTextChanged(CharSequence text, int start, 57 int lengthBefore, int lengthAfter) { 58 super.onTextChanged(text, start, lengthBefore, lengthAfter); 59 buildDrawingCache(); 60 postInvalidate(); 61 } 62 }
主要功能在onDraw方法裏面,先調用setDrawingCacheEnabled(true);讓cache可用,而後經過cache建立一個和原圖片同樣的圖像,經過mMatrix.preScale(1, -1);使圖片倒過來,調用Bitmap.createBitmap(originalImage, 0, height/5, width, height/2, mMatrix, false);建立一個倒過來的圖像,調用canvas.drawBitmap(reflectionImage, 0, height/3f, null);把倒過來的圖像畫到畫布上。經過調用LinearGradient shader = new LinearGradient(0, height/2, 0,
height, 0x7fffffff, 0x0fffffff, TileMode.CLAMP);
mPaint.setShader(shader);
mPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));使倒影的圖像的顏色漸變,由灰色變爲黑色。佈局
時間走動時調用buildDrawingCache();
postInvalidate();post
讓倒影重新繪製。字體
調用setMeasuredDimension(getMeasuredWidth(), (int)(getMeasuredHeight()*1.67));設置圖像的寬度和高度。ui
好了,控件已經寫完了,如今只要在佈局中調用這個控件就能夠在Activity中顯示一個帶有倒影的時間的View了,先寫一個佈局文件:spa
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="#000000" 6 android:paddingTop="@dimen/activity_vertical_margin" > 7 8 <com.alex.reflecttextview.ReflectTextView 9 android:id="@+id/timeView" 10 android:textSize="@dimen/reflect_size" 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:layout_alignParentBottom="true" 14 android:gravity="top|center_horizontal" /> 15 </RelativeLayout>
而後在Activity中顯示這個佈局,我把這個控件的字體重新設置了一下,讓它顯示的方方正正。
1 package com.alex.reflecttextview; 2 3 import android.app.Activity; 4 import android.graphics.Typeface; 5 import android.os.Bundle; 6 import android.view.Window; 7 import android.view.WindowManager; 8 9 public class MainActivity extends Activity { 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 final Window win = getWindow(); 15 win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 16 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 17 win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON 18 | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); 19 setContentView(R.layout.activity_main); 20 TimeView tv = (TimeView) findViewById(R.id.timeView); 21 tv.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/DS-DIGII.TTF")); 22 } 23 }
運行代碼,手機上就回顯示一個帶有倒影的時間View,時間還會走動,是否是很好玩。
好了,就到這裏吧。