Image sliding gradient

Image sliding gradient

Source code introduction: Android image sliding gradient to achieve animation effects.

Source code effect:

Source code snippet:

  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 Full color: 5000 Full gray: 0||10000 Gradient color: 5000~10000
  23. */  
  24. @Override  
  25. public   void draw(Canvas canvas) {
  26.           
  27. int level = getLevel();
  28. if (level == 0 || level == 10000 ) {
  29. // All gray  
  30. mUnSelectedDrawable.draw(canvas);
  31. } else   if (level == 5000 ) {
  32. // Full color  
  33. mSelectedDrawable.draw(canvas);
  34. } else {
  35. // Gradient color (part gray, part color):  
  36. // Get the rectangular boundaries of the current Drawable  
  37. Rect bounds = getBounds();
  38. Rect r = outRect;
  39.               
  40. { // 1. Cut out the left part of the rectangle from the gray image  
  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, // Start cutting from the left or the right  
  49. w, // Width of the target rectangle  
  50. h, // The height of the target rectangle  
  51. bounds, // The original rectangle cut out  
  52. r); // Target rectangle -- the rectangular area required in the final canvas  
  53.   
  54. // Save the prototype of the canvas  
  55. canvas.save();
  56. //Cut out a part of the canvas  
  57. canvas.clipRect(r);
  58. mUnSelectedDrawable.draw(canvas);
  59. // Restore the canvas  
  60. canvas.restore();
  61. }
  62. { // 2. Cut out the right part of the rectangle from the colored image  
  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, // Start cutting from the left or the right  
  71. w, // Width of the target rectangle  
  72. h, // The height of the target rectangle  
  73. bounds, // The original rectangle cut out  
  74. r); // Target rectangle -- the rectangular area required in the final canvas  
  75.   
  76. // Save the prototype of the canvas  
  77. canvas.save();
  78. //Cut out a part of the canvas  
  79. canvas.clipRect(r);
  80. mSelectedDrawable.draw(canvas);
  81. // Restore the canvas  
  82. canvas.restore();
  83. }
  84. }
  85. }
  86.       
  87. @Override  
  88. protected   boolean onLevelChange( int level) {
  89. // Sense the call of setLevel and refresh -- draw()  
  90. invalidateSelf();
  91. return   true ;
  92. }
  93.   
  94. /**
  95. * Initialize data
  96. */  
  97. @Override  
  98. protected   void onBoundsChange(Rect bounds) {
  99. // Define the width and height of two Drawable images -- bound boundaries  
  100. mUnSelectedDrawable.setBounds(bounds);
  101. mSelectedDrawable.setBounds(bounds);
  102. super .onBoundsChange(bounds);
  103. }
  104.   
  105. /**
  106. * Get the actual width and height of the Drawable
  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.   
  133. }

Download address: http://download..com/data/2096556

<<:  Imitation WeChat radar scanning

>>:  Hero Entertainment CEO Ying Shuling: How I influenced Xu Xiaoping and Wang Sicong

Recommend

6 information flow industry cases and delivery data, form costs exposed!

Today, Qingguajun will share with you the analysi...

Why iPhone X may not be the highlight of Apple's press conference?

What is the real highlight of this Apple conferen...

Is starting to live alone a sign of maturity?

© Aging Wisely Blog Leviathan Press: In the movie...

How did ordinary garlic become the anti-cancer "warrior" among condiments?

Audit expert: Wang Guoyi Postdoctoral fellow in N...

Theoretical basis for improving your App startup speed

1. To do a good job, you must first sharpen your ...

Google calls on Apple: Stop secretly modifying user security recommendations

Figure 1: Google AMP pages displayed in Google Se...

vivo Global Mall: E-commerce transaction platform design

1. Background The vivo official mall has undergon...

Divergence or coexistence? A detailed explanation of Android kernel security

1. Knowledge Background In October 2003, Andy Rub...

How to solve the problem of too many users connected to the Win server terminal?

The test succeeded under 2003 and failed under 20...