陪伴而非守候
2018-09-08 23:18:59浏览 1919
package com.example.keweize.surfaceviewwelcome.views;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.example.keweize.surfaceviewwelcome.R;
/**
* Created by keweize on 2016/10/25.
*/
public class DynamicSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private Context context;
private SurfaceHolder mHolder;
private Bitmap bitmap;
private int viewWidth, viewHeight, imgWidth, imgHeight;
//图片在水平位置的偏移量
private float imgOffset = 0;
//动画线程
Thread mDynamicBgThread;
//线程是否开启
private boolean isRunable = false;
//画笔
private Paint mPaint;
//方向标识,标识当前背景图偏移的方向
private boolean moToRight = true;
//动画效果为30侦/秒
private static final long DRAW_INTERVAL = 1000/30;
//每次偏移的偏移量(暂时默认为10)
private static float offset = 20;
public DynamicSurfaceView(Context context) {
this(context, null);
}
public DynamicSurfaceView(Context context, AttributeSet attrs) {
this(context, null, 0);
}
public DynamicSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
mHolder = getHolder();
mHolder.addCallback(this);
//初始化画笔
mPaint = new Paint();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (bitmap == null){
//获得空间宽高
viewWidth = MeasureSpec.getSize(widthMeasureSpec);
viewHeight = MeasureSpec.getSize(heightMeasureSpec);
//初始化背景图(背景图压缩至跟view一样的高度)
initBitmap();
}
}
private void initBitmap(){
bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.bg1);
float scaleNum = (float) viewHeight / (float) bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.postScale(scaleNum, scaleNum); //长和宽放大缩小的比例
//得到与view控件相同高度的bitmap对象
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
imgWidth = bitmap.getWidth();
imgHeight = bitmap.getHeight();
//因为偏移量如果固定的话,在分辨率高的手机上会走得很慢,在分辨率低的手机会跑得很快,所以这里直接动态设置单次偏移量(每个轮回20s走完)
offset = (float) (imgWidth - viewWidth) / ((float) 20 * (float) 30);
Log.i("DynamicSurfaceView", "offset is " + offset);
Log.i("DynamicSurfaceView", " afterCompress: bitmap 's width is" + bitmap.getWidth() + " | height is " + bitmap.getHeight());
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
isRunable = true;
mDynamicBgThread = new Thread(new DynamicBgThread());
mDynamicBgThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
isRunable = false;
}
//写一个进程来动态绘制背景
private class DynamicBgThread implements Runnable{
@Override
public void run() {
long deltaTime = 0;
long tickTime = 0;
tickTime = System.currentTimeMillis();
while (isRunable) {
Canvas canvas = null;
try {
synchronized (mHolder) {
canvas = mHolder.lockCanvas();
drawBackground(canvas);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != mHolder) {
mHolder.unlockCanvasAndPost(canvas);
}
}
deltaTime = System.currentTimeMillis() - tickTime;
if(deltaTime < DRAW_INTERVAL) {
try {
Thread.sleep(DRAW_INTERVAL - deltaTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
tickTime = System.currentTimeMillis();
}
}
}
//绘制背景
private void drawBackground(Canvas canvas){
canvas.drawBitmap(bitmap, -imgOffset, 0, mPaint);
if (moToRight){
if (viewWidth + imgOffset + offset < imgWidth){
imgOffset += offset;
}else {
moToRight = false;
}
}else {
if (imgOffset > 0){
imgOffset -= offset;
}else {
moToRight = true;
}
}
}
}
原文链接:http://www.apkbus.com/blog-880881-68585.html