Play GIF animation using SurfaceView

The words written in the front: Summer vacation is available for internship, I have added a few Android recruitment groups for boredom, it doesn’t matter if you don’t, there is a crying cry inside I couldn’t find a job, and it was hard to find a job. After adding it, I was scared to download a few recruitment apps and cast a bunch of resumes, but few responded to me. . . Give me a feeling: As the group said, Android development is saturated. . . No way, write some blogs every day while reviewing knowledge and preparing for the interview. .

SurfaceView:

SurfaceView is generally compatible with SurfaceViewHolder (equivalent to a butler of SurfaceView ) Used in combination, SurfaceHolder is used to draw on the SurfaceView associated with it, SurfaceView.getHolder().

SurfaceHolder provides the following methods to obtain the Canvas object.

Canvas lockCanvas(); Lock the S~w object and get its canvas.

Canvas lockCanvas(Rect dirty); Lock the area divided by Rect on it, and get its Canvas.

Oh, by the way, SurfaceHolder generally needs to add a CallBack to get the status of SurfaceView, because we don’t know when the SurfaceView is drawn, we need him to tell.

public class GifSurfaceView extends SurfaceView implements Callback {private SurfaceHolder holder; private String path; private float scale; /** * gif animation start coordinates */ private int x; private int y; Handler mHandler = new Handler(); private Runnable run = new Runnable() {@Override public void run() {Canvas canvas = holder.lockCanvas(); canvas.save(); //To zoom canvas.scale(1f/scale, 1f/scale); movie.draw(canvas, x, y); canvas.restore(); holder. unlockCanvasAndPost(canvas); //Set time: 1 2 3 4 5 1 2 3 4 5 movie.setTime((int)(System.currentTimeMillis()% movie.duration())); mHandler.removeCallbacks(run); mHandler .postDelayed(run, 20);//Let it keep drawing} }; private Movie movie;//Each gif can be regarded as a small movie. public GifSurfaceView(Context context,String path) {super(context); holder = getHolder();// movie player holder.addCallback(this); this.path = path;} //surfaceView has changed @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {} //SurfaceView was created successfully @Override public void surfaceCreated(SurfaceHolder arg0) {initView();} /** * Create a gif container, and at the same time according to the container The width and height of the gif and the width and height of the gif are scaled and centered */ private void initView() {try {movie = Movie.decodeStream(getContext().getAssets().open(path)); int width = movie.width (); int height = movie.height(); float scaleX = width / (float)this.getWidth();//1 160/200.0=0 float scaleY = height / (float)this.getHeight(); /* * * Determine the ratio of our gif zoom * * The gif picture is displayed in the center of the surfaceView */ if(scaleX>scaleY){ x = 0; y = (int)((getHeight()*scaleX-height)/2); scale = scaleX; }else{ y = 0; x = (int)((getWidth()*scaleY)/2); scale = scaleY;} mHandler.post(run);} catch (IOException e) {// TODO Auto- generated catch block e.printStackTrace();}} ////surfaceView is destroyed @Override public void surfaceDestroyed(SurfaceHolder arg0) {mHandler.removeCallbacks(run); })

Leave a Comment

Your email address will not be published.