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加载长图的更多相关文章

  1. Android内存优化————加载长图

    项目中总会遇到加载长图的需求,图片的长度可能是手机长度的很多倍,也就是需要通过滑动来查看图片.比较简单的实现方式就是使用ScrollView来加载长图,但是这样做有一个很严重的问题,就是内存消耗严重. ...

  2. ImageView加载长图(适用不需要缩放的情况)

    此案例适用于加载网络长图且图片的宽和高已知的情况.由于ImageView加载图片有一个4096*4096的限制,所以对于巨长图的加载比较麻烦,需要我们自己去手动处理. 有两种解决方案:第一种就是比较l ...

  3. Android 高清加载巨图方案 拒绝压缩图片

    Android 高清加载巨图方案 拒绝压缩图片 转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/49300989: 本文出自:[张 ...

  4. Unity使用脚本进行批量动态加载贴图

    先描述一下我正在做的这个项目,是跑酷类音游. 那么跑酷类音游在绘制跑道上的时候,就要考虑不同的砖块显示问题.假设我有了一个节奏列表,那么我们怎么将不同的贴图贴到不同的砖块上去呢? 我花了好几个小时才搞 ...

  5. 简易仿ios菊花加载loading图

    原文链接:https://mp.weixin.qq.com/s/wBbQgOfr59wntNK9ZJ5iRw 项目中经常会用到加载数据的loading显示图,除了设计根据app自身设计的动画loadi ...

  6. React-Native 之 GD (二十)removeClippedSubviews / modal放置的顺序 / Android 加载git图\动图 / 去除 Android 中输入框的下划线 / navigationBar

    1.removeClippedSubviews 用于提升大列表的滚动性能.需要给行容器添加样式overflow:’hidden’.(Android已默认添加此样式)此属性默认开启 这个属性是因为在早期 ...

  7. H5异步加载多图

    异步加载多图(可能没啥用,加载慢)(图片预加载,提前给浏览器缓存图片) 1. 用一个计数变量记录需要加载的图片个数 2. 用new Image()去加载,加载完给此对象的src赋值要加载的url路径( ...

  8. Android 高清加载巨图方案, 拒绝压缩图片

    源地址:http://blog.csdn.net/lmj623565791/article/details/49300989 一.概述 距离上一篇博客有段时间没更新了,主要是最近有些私事导致的,那么就 ...

  9. django模型层优化(关联对象) 懒加载和预加载 +长链接

    懒加载 存在于外键和多对多关系不检索关联对象的数据调用关联对象会再次查询数据库 问题根源 查看django orm的数据加载,两次. 查询user,查询menu 预加载的方法 预加载单个关联对象--s ...

随机推荐

  1. 修复Thinkphp框架5.0和5.1版本的远程代码执行安全漏洞

    由于框架对控制器名没有进行足够的检测会导致在没有开启强制路由的情况下可能的getshell漏洞.最直接的影响为index.php直接被篡改成首页html的内容! 5.0版本 thinkphp/libr ...

  2. 移动端自动化测试-WTF Appium

    手机App分为两大类,原生App(Native App)和混合APP(Hybrid App) 原生App(Native App) 原生App实际就是我们所常见的传统App开发模式,云端数据存储+App ...

  3. eas之kdtable格式化

    设置表.列.行或单元的格式化字符串 // 设置表table.getStyleAttributes().setNumberFormat(formatString); // 设置列column.getSt ...

  4. C#第八节课

    for的穷举迭代.while.do  while using System;using System.Collections.Generic;using System.Linq;using Syste ...

  5. 51nod1081 子段求和

    给出一个长度为N的数组,进行Q次查询,查询从第i个元素开始长度为l的子段所有元素之和. 例如,1 3 7 9 -1,查询第2个元素开始长度为3的子段和,1 {3 7 9} -1.3 + 7 + 9 = ...

  6. [luogu 1092] 虫食算 (暴力搜索剪枝)

    传送门 Description Input 包含四行. 第一行有一个正整数 (N≤26). 后面的三行,每行有一个由大写字母组成的字符串,分别代表两个加数以及和.这3个字符串左右两端都没有空格,从高位 ...

  7. Spring Web Flow实例教程

    目录: 参考文献 购物车用例 什么情况下可以使用 Spring Web Flow? 配置 Spring Web MVC 配置 Spring Web Flow 2.0 的基础 在购物车示例应用中配置 S ...

  8. elasticsearch 权威指南搜索阅读笔记(四)

    多索引多type搜索 分页搜索 每页5条 查询一到3页数据 第一页:http://127.0.0.1:9200/blogs2/product/_search?size=5&from=0 第二页 ...

  9. oracle 单独开始一个事物的写法 。

    SET TRANSACTION NAME 'Update salaries'; SAVEPOINT before_salary_update; UPDATE employees SET salary= ...

  10. 哈哈,找到一种方式来简单模拟EXTJS中与服务器的AJAX交互啦。

    一直在测试客户端的EXTJS,但遇到服务器端就麻烦了,要建库,要写JSON,要有HTTP返回值. 今天测试了一个简单的方法,经过测试是OK了. 那,就是Python的SimpleHTTPServer模 ...