• SurfaceView加载长图


    1:SurfaceView加载长图,移到。可以充当背景 效果截图

    2:View (淡入淡出动画没实现:记录下)

    package com.guoxw.surfaceviewimage;
    
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    
    import java.util.Timer;
    import java.util.TimerTask;
    
    /**
     * Created by guoxw on 2017/12/6.
     * SurfaceView 加载长图缓慢移动显示,充当背景。
     */
    
    public class SurfaceViewImage extends SurfaceView implements SurfaceHolder.Callback {
    
        private static final String TAG = "SurfaceViewTest1";
        private SurfaceHolder surfaceHolder = null;
        private int imageH = 0, imageW = 0;
        private Bitmap bitmap = null;
        private Paint mBitPaint = null;
        /***每次移动相对于起始点的距离*/
        private int gap = 0;
        private Timer timer;
        private TimerTask timerTask;
        private int surfaceW, surfaceH;
        private ScroListener scroListener = null;
        private Canvas canvas;
        /***设置透明度  0-255 */
        private int alphaValueAdd = 0;
        private int alphaValueDec = 255;
        private boolean isVisibility = true;
        private int transType = 1;
    
        public SurfaceViewImage(Context context, AttributeSet attrs) {
            super(context, attrs);
            Log.d(TAG, "SurfaceViewTest1: ");
            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bannerbg2);
            imageH = bitmap.getHeight();
            imageW = bitmap.getWidth();
            mBitPaint = new Paint();
            mBitPaint.setColor(Color.RED);
            mBitPaint.setAlpha(alphaValueDec);
            Log.d(TAG, "SurfaceViewTest1: --寛高:" + imageW + "--" + imageH);
            surfaceHolder = getHolder();
            surfaceHolder.addCallback(this);
    
    
        }
    
    
        private void imageDraw() {
            Log.d(TAG, "imageDraw: ");
            switch (transType) {
                case 1:
                    canvas = surfaceHolder.lockCanvas(new Rect(0, 0, surfaceW, surfaceW));
                    if (gap < (imageW - surfaceW)) {
                        if (alphaValueAdd <= 255) {
                            alphaValueAdd += 5;
                        } else {
                            gap += 1;
                        }
                    } else if (gap >= (imageW - surfaceW)) {
                        alphaValueDec -= 5;
                        scroListener.scroTopListener();
                        if (alphaValueDec == 0) {
                            Log.d(TAG, "imageDraw: 次数- 000");
                            // scroListener.scroTopListener();
                        }
                    }
                    if (alphaValueAdd <= 255) {
                        mBitPaint.setAlpha(alphaValueAdd);
                    } else {
                        mBitPaint.setAlpha(alphaValueDec);
                    }
                    Rect srcRect = new Rect(0 + gap, 1000, surfaceW + gap, surfaceH + 1000);
                    Rect dstRect = new Rect(0, 0, surfaceW, surfaceH);
                    Log.d(TAG, "imageDraw: image数据alpha值:--" + alphaValueDec);
                    canvas.drawBitmap(bitmap, srcRect, dstRect, mBitPaint);
                    surfaceHolder.unlockCanvasAndPost(canvas);
                    break;
                case 2:
                    break;
                case 3:
                    break;
            }
    
        }
    
    
        public void setBitmap(Bitmap bitmap) {
            resetImageData();
            this.bitmap = bitmap;
        }
    
        private void resetImageData() {
            imageH = bitmap.getHeight();
            imageW = bitmap.getWidth();
            gap = 0;
            alphaValueDec = 255;
            alphaValueAdd = 0;
        }
    
    
        /**
         * 移动完时,设置监听,更换image
         * @param scroListener
         */
        public void setScroListener(ScroListener scroListener) {
            this.scroListener = scroListener;
        }
    
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            surfaceH = getHeight();
            surfaceW = getWidth();
            Log.d(TAG, "surfaceCreated: 控件的寛高:" + surfaceW + "---" + surfaceH); // 1196*574
            timer = new Timer();
            timerTask = new TimerTask() {
                @Override
                public void run() {
    
                    while (isVisibility) {
                        imageDraw();
                    }
    
    
                }
            };
            timer.schedule(timerTask, 100, 60);
    
        }
    
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    
            Log.d(TAG, "surfaceChanged: ");
    
        }
    
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            Log.d(TAG, "surfaceDestroyed: ");
            try {
                Canvas canvas = surfaceHolder.lockCanvas(null);
                canvas.drawColor(Color.BLACK);
                surfaceHolder.unlockCanvasAndPost(canvas);
            } catch (Exception ex) {
            }
    
    
        }
    
    
        public interface ScroListener {
            void scroTopListener();
        }
    
    
        public void destory() {
    
            if (timer != null) {
                timer.cancel();
                timerTask = null;
                timer = null;
            }
    
        }
    
    }
    

     3.修改下

    public class SurfaceViewImage extends SurfaceView implements SurfaceHolder.Callback {
    
        private static final String TAG = "SurfaceViewTest1";
        private SurfaceHolder surfaceHolder = null;
        private int imageH = 0, imageW = 0;
        private Bitmap bitmap = null;
        private Paint mBitPaint = null;
        /***每次移动相对于起始点的距离*/
        private int gap = 0;
        private int oneGap=2;
        private Timer timer;
        private TimerTask timerTask;
        private int surfaceW, surfaceH;
        private Canvas canvas;
        public  boolean isDraw = false;
    
        /*** 正方向移动完毕后,反向移动,如此循环*/
        private boolean PosDirection=true;
    
        public SurfaceViewImage(Context context, AttributeSet attrs) {
            super(context, attrs);
            Log.d(TAG, "SurfaceViewTest1: ");
            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.banner2);
            imageH = bitmap.getHeight();
            imageW = bitmap.getWidth();
            mBitPaint = new Paint();
            mBitPaint.setColor(Color.RED);
            Log.d(TAG, "SurfaceViewTest1: --寛高:" + imageW + "--" + imageH);
            surfaceHolder = getHolder();
            surfaceHolder.addCallback(this);
    
    
        }
    
    
        private void imageDraw() {
                    if(isDraw){
                        canvas = surfaceHolder.lockCanvas(new Rect(0, 0, surfaceW, imageH));
                        if(PosDirection){
                            if (gap < (imageW - surfaceW)) {
                                gap += oneGap;
                            } else if (gap >= (imageW - surfaceW)) {
                                PosDirection=false;
                            }
                        }else {
    
                            if (gap >0) {
    
                                gap -= oneGap;
    
                            } else if (gap <=0) {
    
                                PosDirection=true;
                            }
                        }
                        Rect srcRect = new Rect(0 + gap, 0, surfaceW + gap, imageH);
                        Rect dstRect = new Rect(0, 0, surfaceW, surfaceH);
    
                        canvas.drawBitmap(bitmap, srcRect, dstRect, mBitPaint);
                        surfaceHolder.unlockCanvasAndPost(canvas);
    
    
    
    
    
    
    
            }
    
        }
    
    
        public void setBitmap(Bitmap bitmap) {
            Log.d(TAG, "setBitmap: change photo");
            isDraw = false;
            try {
                Thread.sleep(100);
                //this.bitmap.recycle();
                Log.d(TAG, "setBitmap: change photo1");
    
                this.bitmap = bitmap;
                resetImageData();
                Log.d(TAG, "setBitmap: change photo2");
    
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            isDraw = true;
            Log.d(TAG, "setBitmap: change photo3");
    
        }
    
        private void resetImageData() {
            imageH = bitmap.getHeight();
            imageW = bitmap.getWidth();
            gap = 0;
        }
    
    
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            Log.d(TAG, "surfaceCreated: ");
    
            //if(bitmap==null){
            //    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.banner2);
           // }
            surfaceH = getHeight();
            surfaceW = getWidth();
            isDraw=true;
            timer = new Timer();
            timerTask = new TimerTask() {
                @Override
                public void run() {
                    if (isDraw) {
                        imageDraw();
                    }
                }
            };
            timer.schedule(timerTask, 300, 50);
    
        }
    
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    
            Log.d(TAG, "surfaceChanged: ");
    
        }
    
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            Log.d(TAG, "surfaceDestroyed: ");
            isDraw=false;
    
            try {
                Thread.sleep(60);
                // Canvas canvas = surfaceHolder.lockCanvas(null);
                // canvas.drawColor(Color.BLACK);
               // surfaceHolder.unlockCanvasAndPost(canvas);
                timerTask.cancel();
                timer.cancel();
                timerTask=null;
                timer=null;
              //  bitmap.recycle();
              //  bitmap=null;
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
    
        }
    
    
    }
    

      

     Code: 

    https://pan.baidu.com/s/1o7XYOsy     SurfaceViewImage.zip
    

      

  • 相关阅读:
    JavaScript-----7.循环
    3. CSS新特性之动画
    JavaScript-----6.流程控制:分支
    JavaScript-----5.运算符
    JavaScript-----4.数据类型
    2. CSS新特性之2D转换transform
    JavaScript-----3.变量
    JavaScript-----2初识
    JavaScript---1.计算机的编程基础
    品优购学习心得
  • 原文地址:https://www.cnblogs.com/galibujianbusana/p/8044406.html
Copyright © 2020-2023  润新知