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; } } }
Code:
https://pan.baidu.com/s/1o7XYOsy SurfaceViewImage.zip
时间: 2024-10-12 19:28:26