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

Meitu XiuXiu product analysis report!

When it comes to mobile phone photo editing softw...

Kuaishou Search for Hidden Traffic Opportunities

When users' content consumption needs extend ...

How to improve users’ acceptance of advertising? Here are 7 techniques!

Those great advertisements often come from the bra...

Airbag: Can “explosion” save lives?

Author: Xue Bin When we think of explosions, we o...

What is a dead link? What does a dead link mean?

What is a dead link? What does a dead link mean? ...

New power grid, all relying on "wind and solar"? Unveiling the new power system

In your life, are you promoting such behaviors th...

After nearly 20 years, the all-new BMW 8 Series will debut tomorrow night

The new generation BMW 8 Series will make its glo...

Did you know that people who love to chat are less likely to develop dementia?

Author: Sun Taixin, Chief Physician, Beijing Elec...

Why is "Wolf Warrior 2" so popular? Because Wu Jing understands operations!

Wu Jing is a relatively unknown martial arts acto...