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
SurfaceView加载长图的更多相关文章
- Android内存优化————加载长图
项目中总会遇到加载长图的需求,图片的长度可能是手机长度的很多倍,也就是需要通过滑动来查看图片.比较简单的实现方式就是使用ScrollView来加载长图,但是这样做有一个很严重的问题,就是内存消耗严重. ...
- ImageView加载长图(适用不需要缩放的情况)
此案例适用于加载网络长图且图片的宽和高已知的情况.由于ImageView加载图片有一个4096*4096的限制,所以对于巨长图的加载比较麻烦,需要我们自己去手动处理. 有两种解决方案:第一种就是比较l ...
- Android 高清加载巨图方案 拒绝压缩图片
Android 高清加载巨图方案 拒绝压缩图片 转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/49300989: 本文出自:[张 ...
- Unity使用脚本进行批量动态加载贴图
先描述一下我正在做的这个项目,是跑酷类音游. 那么跑酷类音游在绘制跑道上的时候,就要考虑不同的砖块显示问题.假设我有了一个节奏列表,那么我们怎么将不同的贴图贴到不同的砖块上去呢? 我花了好几个小时才搞 ...
- 简易仿ios菊花加载loading图
原文链接:https://mp.weixin.qq.com/s/wBbQgOfr59wntNK9ZJ5iRw 项目中经常会用到加载数据的loading显示图,除了设计根据app自身设计的动画loadi ...
- React-Native 之 GD (二十)removeClippedSubviews / modal放置的顺序 / Android 加载git图\动图 / 去除 Android 中输入框的下划线 / navigationBar
1.removeClippedSubviews 用于提升大列表的滚动性能.需要给行容器添加样式overflow:’hidden’.(Android已默认添加此样式)此属性默认开启 这个属性是因为在早期 ...
- H5异步加载多图
异步加载多图(可能没啥用,加载慢)(图片预加载,提前给浏览器缓存图片) 1. 用一个计数变量记录需要加载的图片个数 2. 用new Image()去加载,加载完给此对象的src赋值要加载的url路径( ...
- Android 高清加载巨图方案, 拒绝压缩图片
源地址:http://blog.csdn.net/lmj623565791/article/details/49300989 一.概述 距离上一篇博客有段时间没更新了,主要是最近有些私事导致的,那么就 ...
- django模型层优化(关联对象) 懒加载和预加载 +长链接
懒加载 存在于外键和多对多关系不检索关联对象的数据调用关联对象会再次查询数据库 问题根源 查看django orm的数据加载,两次. 查询user,查询menu 预加载的方法 预加载单个关联对象--s ...
随机推荐
- Python这些问题你会吗?
inal作用域的代码一定会被执行吗? 正常的情况下,finally作用域的代码一定会被执行的,不管是否发生异常.哪怕是调用了sys.exit函数,finally也是会被执行的,那怎么样才能让final ...
- eas之编码规则&单据转换规则
*当在企业建模中没有要显示的项目的话,则从包更新到系统树然后选择到规则定义,对申请单新增规则. 企业建模--业务规则-规则定义组织优先 多组织有先 集团优先固定值 显示格式PUR ..系统日期 2 ...
- eas之常用源码整理
//查看是否有相关权限 boolean hasAllotPermission= PermissionFactory.getRemoteInstance().hasFunctionPer ...
- [luogu4799 CEOI2015 Day2] 世界冰球锦标赛(折半搜索)
传送门 Solution 折半搜索裸题,注意\(long long\) Code #include <cmath> #include <cstdio> #include < ...
- [cf 1015f] Bracket Substring (dp+kmp)
传送门 Solution 设dp方程dp[now][pos][red][fla]表示还有now个位置,pos表示匹配到第几位,red表示左括号数-右括号数,fla表示是否已经是给定串的字串 暴力转移即 ...
- 传输模型,网络层次划分,三次握手,四次挥手,IP与端口,套接字socket
了解套接字之前,需要先了解基本的传输模型 其次,还需要了解网络的七层划分和四层结构 在python中,数据链路层相当于硬件层,python不需要了解,只用在传输层进行学习就足够了 其中,传输层分为TC ...
- 在做公司项目是时,昨天晚上还好的,但是第二天启动tomcat发现tomcat启动了,但是没把项目启动起来
1.问题:在做公司项目是时,昨天晚上还好的,但是第二天启动tomcat发现tomcat启动了,但是没把项目启动起来 2.问题排除: 1)昨天晚上还好着呢,并且没改动代码,排除代码问题.日志中无报错信息 ...
- HDU 1569 方格取数(2)
方格取数(2) Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 15 ...
- orcale 单行函数之数字函数, 日期函数
日期函数: 案例:
- Bi-shoe and Phi-shoe 欧拉函数 素数
Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular co ...