android view realizes the fading effect of a picture

  • 2021-09-12 02:24:50
  • OfStack

Because of an Android project, two photos should be displayed in turn in the software opening interface. I don't want to replace two pictures too stiffly, so let one picture fade away and the second picture be displayed gradually.

There are three methods,

Type 1:

Make a few fading pictures, and the transparency will be from 255 to 0. In this way, the pictures with different transparency will be displayed in turn, and the fading effect will appear. However, this method wastes resources and abandons them. (Painting in view)

Type 2:

Use only one picture, and save the pixel information of every one point in the picture into an array. Every one point pixel is ARGB, just 32 bits, and put it into a value of int type. Then change the size of the upper 8 bits of int value to realize the change of alpha value. After changing the array information, create a new picture.

This paper focuses on this method. (Painting in view)

Type 3:

The first two are implemented in view, and can also be implemented in layout, using imageswitcher and in-and-out animation. This method is introduced in the next article.

The effect to be achieved:

After the first picture is displayed for 1 second, it gradually hides, and the second picture appears.

The effect is very simple, so there is no mapping. A total of two files, one activity1 and one view.

The code is presented:

Activity:


package liu.com.kiexun;
 
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
 
public class SimpleTestActivity extends Activity {
  /** Called when the activity is first created. */
 SimpleFlash simpleFlash;
 boolean flag=true;
 private Handler handler=new Handler()
 {
  public void handleMessage(Message msg)
  {
  switch(msg.what)
  {
   case 1:
   if (flag)
   {
    try
    {
    Thread.sleep(1000);// No. 1 1 The time of the picture is displayed as 1 Seconds 
    } catch (InterruptedException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    flag=false;
   }  
   simpleFlash.invalidate(); 
   break;  
   default:
   break;  
  }  
  }; 
 };
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // It came into being here contex , can be right view Initialize 
    simpleFlash=new SimpleFlash(this,handler);
    setContentView(simpleFlash);
  }
}

view:


package liu.com.kiexun;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
 
public class SimpleFlash extends View
{
 
 int index=0;
 int size=480*800;
 Bitmap firstBitmap,secondBitmap;
 Canvas canvas=null;
 int pixels[]=new int[size];
 Handler handler ;
 int changeArrary[]=
  {
  (1<<31)-1,
 
  (1<<30)-1,
  
  (1<<29)-1,
  
  (1<<28)-1,
  
  (1<<27)-1,
  
  (1<<26)-1,
  
  (1<<25)-1,
  
  (1<<24)-1
  };
 
 int changeArrary2[]={
/*  11111110   
  11111100  
  11111000 
  11110000 
  11100000   
  11000000   
  10000000   
    
  
  01111111
  00111111
  00011111
  00001111
   00000111
   00000011
  00000001
  00000000
   
  1<<32  Equivalent to no displacement  >=32 The number of times and shifts of bits is and 32 Remainder of 
 */
 
  ( ( (1<<31)-1 )+(1<<31)-(1<<24) ),
  ( ( (1<<31)-1 )+(1<<31)-(1<<24)-(1<<25) ),
  ( ( (1<<31)-1 )+(1<<31)-(1<<24)-(1<<25)-(1<<26) ),
  ( ( (1<<31)-1 )+(1<<31)- (1<<24)-(1<<25)-(1<<26)-(1<<27)),
  ( ( (1<<24)-1 )+(1<<31)+(1<<30)+(1<<29)),
  ( ( (1<<24)-1 )+(1<<31)+(1<<30)),
  ( ( (1<<24)-1 )+(1<<31)),
  (1<<31)-1,
  
  (1<<30)-1,
  
  (1<<29)-1,
  
  (1<<28)-1,
  
  (1<<27)-1,
  
  (1<<26)-1,
  
  (1<<25)-1,
  (1<<24)-1
  
 };
 public SimpleFlash(Context context,Handler handler)
 {
 super(context);
 this.handler=handler;
 // TODO Auto-generated constructor stub
 firstBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.about);
 secondBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.help);
 firstBitmap.getPixels(pixels, 0, 480, 0, 0, 480, 800);
 }
 /*
 * (non-Javadoc)
 * @see android.view.View#onDraw(android.graphics.Canvas)
 * draw Function to display the picture after the completion of the execution, should be the completion of the execution before submitting the painting message 
 */
 public void onDraw(Canvas canvas)
 {
 this.canvas=canvas;
 canvas.drawBitmap(secondBitmap, 0, 0, null);
 /*
  *  Will not be displayed first 2 A picture, 5 After seconds, the first 1 Pictures 
  */
 firstBitmap=Bitmap.createBitmap(pixels, 480, 800,Config.ARGB_8888);
 canvas.drawBitmap(firstBitmap, 0, 0, null); 
 changePixels();
 try
 {
  Thread.sleep(100);
 } catch (InterruptedException e)
 {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 }
 
 public void changePixels()
 {
 if (index<8)
 {
  for (int i = 1; i < pixels.length; i++)
  {
  pixels[i]=pixels[i]&changeArrary[index];
  }
  index++;
  Message changeMessage=new Message();
  changeMessage.what=1;
  handler.sendMessage(changeMessage); 
 }
 }
 
}

Related articles: