自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

圖片滑動(dòng)漸變

移動(dòng)開發(fā)
Android圖片滑動(dòng)漸變,實(shí)現(xiàn)動(dòng)畫特效。

源碼簡(jiǎn)介:Android圖片滑動(dòng)漸變,實(shí)現(xiàn)動(dòng)畫特效。

源碼效果:

源碼片段:

  1. package com.example.tz_demo_8_14; 
  2.   
  3. import android.graphics.Canvas; 
  4. import android.graphics.ColorFilter; 
  5. import android.graphics.Rect; 
  6. import android.graphics.drawable.Drawable; 
  7. import android.util.Log; 
  8. import android.view.Gravity; 
  9.   
  10. public class RevealDrawable extends Drawable { 
  11.   
  12.     private Drawable mUnSelectedDrawable; 
  13.     private Drawable mSelectedDrawable; 
  14.     private Rect outRect = new Rect(); 
  15.   
  16.     public RevealDrawable(Drawable unSelectedDrawable, Drawable selectedDrawable) { 
  17.         this.mUnSelectedDrawable = unSelectedDrawable; 
  18.         this.mSelectedDrawable = selectedDrawable; 
  19.     } 
  20.   
  21.     /** 
  22.      * level:0~10000 全彩色:5000 全灰色:0||10000 漸變色:5000~10000 
  23.      */ 
  24.     @Override 
  25.     public void draw(Canvas canvas) { 
  26.           
  27.         int level = getLevel(); 
  28.         if (level == 0 || level == 10000) { 
  29.             // 全灰色 
  30.             mUnSelectedDrawable.draw(canvas); 
  31.         } else if (level == 5000) { 
  32.             // 全彩色 
  33.             mSelectedDrawable.draw(canvas); 
  34.         } else { 
  35.             // 漸變色(一部分灰色,一部分彩色): 
  36.             // 得到當(dāng)前Drawable的矩形邊界 
  37.             Rect bounds = getBounds(); 
  38.             Rect r = outRect; 
  39.               
  40.             {// 1.從灰色的圖片摳出左邊的部分矩形 
  41.                 // level:0~5000~10000 
  42.                 float ratio = (level / 5000f) - 1f; 
  43.                 int w = bounds.width(); 
  44.                 w = (int) (w * Math.abs(ratio)); 
  45.                 int h = bounds.height(); 
  46.                 int gravity = ratio < 0 ? Gravity.LEFT : Gravity.RIGHT; 
  47.   
  48.                 Gravity.apply(gravity, // 從左邊開始切還是從右邊 
  49.                         w, // 目標(biāo)矩形的寬 
  50.                         h, // 目標(biāo)矩形的高 
  51.                         bounds, // 被摳出來(lái)的原矩形 
  52.                         r);// 目標(biāo)矩形 -- 最終畫布里面需要的矩形區(qū)域 
  53.   
  54.                 // 保存畫布的原型 
  55.                 canvas.save(); 
  56.                 // 將畫布裁剪一部分出來(lái) 
  57.                 canvas.clipRect(r); 
  58.                 mUnSelectedDrawable.draw(canvas); 
  59.                 // 恢復(fù)畫布 
  60.                 canvas.restore(); 
  61.             } 
  62.             {// 2. 從彩色的圖片摳出右邊的部分矩形 
  63.                 // level:0~5000~10000 
  64.                 float ratio = (level / 5000f) - 1f; 
  65.                 int w = bounds.width(); 
  66.                 w -= (int) (w * Math.abs(ratio)); 
  67.                 int h = bounds.height(); 
  68.                 int gravity = ratio < 0 ? Gravity.RIGHT : Gravity.LEFT; 
  69.   
  70.                 Gravity.apply(gravity, // 從左邊開始切還是從右邊 
  71.                         w, // 目標(biāo)矩形的寬 
  72.                         h, // 目標(biāo)矩形的高 
  73.                         bounds, // 被摳出來(lái)的原矩形 
  74.                         r);// 目標(biāo)矩形 -- 最終畫布里面需要的矩形區(qū)域 
  75.   
  76.                 // 保存畫布的原型 
  77.                 canvas.save(); 
  78.                 // 將畫布裁剪一部分出來(lái) 
  79.                 canvas.clipRect(r); 
  80.                 mSelectedDrawable.draw(canvas); 
  81.                 // 恢復(fù)畫布 
  82.                 canvas.restore(); 
  83.             } 
  84.         } 
  85.     } 
  86.       
  87.     @Override 
  88.     protected boolean onLevelChange(int level) { 
  89.         // 感知setLevel的調(diào)用,然后刷新 -- draw() 
  90.         invalidateSelf(); 
  91.         return true
  92.     } 
  93.   
  94.     /** 
  95.      * 初始化數(shù)據(jù) 
  96.      */ 
  97.     @Override 
  98.     protected void onBoundsChange(Rect bounds) { 
  99.         // 定義兩個(gè)Drawable圖片的寬高 -- bound邊界 
  100.         mUnSelectedDrawable.setBounds(bounds); 
  101.         mSelectedDrawable.setBounds(bounds); 
  102.         super.onBoundsChange(bounds); 
  103.     } 
  104.   
  105.     /** 
  106.      * 得到Drawable的實(shí)際寬高 
  107.      */ 
  108.     @Override 
  109.     public int getIntrinsicWidth() { 
  110.         return mSelectedDrawable.getIntrinsicWidth(); 
  111.     } 
  112.   
  113.     @Override 
  114.     public int getIntrinsicHeight() { 
  115.         return mSelectedDrawable.getIntrinsicHeight(); 
  116.     } 
  117.   
  118.     @Override 
  119.     public void setAlpha(int alpha) { 
  120.   
  121.     } 
  122.   
  123.     @Override 
  124.     public void setColorFilter(ColorFilter cf) { 
  125.   
  126.     } 
  127.   
  128.     @Override 
  129.     public int getOpacity() { 
  130.         return 0
  131.     } 
  132.   

下載地址:http://down.51cto.com/data/2096556

責(zé)任編輯:倪明 來(lái)源: devstore
相關(guān)推薦

2012-04-06 13:52:58

ASP.NET

2011-04-22 11:24:13

mootools

2023-06-07 10:41:43

2013-10-16 16:17:15

iOS開發(fā)優(yōu)化方案

2023-05-08 09:08:33

CSS前端

2014-07-22 10:58:33

HTML5jQuery

2015-10-20 15:54:16

android源碼滑動(dòng)關(guān)閉

2017-05-03 16:30:38

AndroidScrollView滾動(dòng)視圖

2021-09-01 08:32:13

CSS 技巧@property

2011-07-18 13:06:18

漸變維度數(shù)據(jù)庫(kù)

2013-06-20 11:10:24

iOS開發(fā)UItableView單元格背景漸變

2015-09-22 10:53:27

引導(dǎo)頁(yè)

2023-06-05 09:28:32

CSS漸變

2023-02-24 08:32:50

CSS漸變屬性

2022-05-27 14:55:34

canvas畫布鴻蒙

2022-10-27 09:13:58

CSSGradient

2014-12-31 14:52:27

SwipeMenuLiSwipeMenu

2015-07-17 10:51:01

滑動(dòng)菜單

2015-02-28 15:55:32

手勢(shì)gesture過(guò)渡

2023-08-11 07:44:40

TCP滑動(dòng)窗口數(shù)據(jù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)