Android仿微博加载长图滚动查看效果
本文实例为大家分享了Android加载长图片的具体代码,供大家参考,具体内容如下
解决步骤
1.将图片缩放到与控件等宽
2.判断缩放后的图片高度,如果高度大于控件高度较多(这里设置的是1.5倍),认定为长图,可滑动查看图片
|-如果高度小于控件高度的1.5倍,以控件高度为基准,重新缩放图片
packageorg.wandcf_ces.fairproject.widgets;
importandroid.annotation.TargetApi;
importandroid.content.Context;
importandroid.graphics.Bitmap;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Matrix;
importandroid.graphics.Paint;
importandroid.graphics.Rect;
importandroid.graphics.RectF;
importandroid.os.Build;
importandroid.support.annotation.Nullable;
importandroid.util.AttributeSet;
importandroid.view.MotionEvent;
importandroid.view.View;
/**
*Createdbysunruion2017/3/8.
*加载长图片
*解决步骤
*1.将图片缩放到与控件等宽
*2.判断缩放后的图片高度,如果高度大于控件高度较多(这里设置的是1.5倍),认定为长图,可滑动查看图片
*|-如果高度小于控件高度的1.5倍,以控件高度为基准,重新缩放图片
*
*/
publicclassLongImageViewextendsView{
privateintwidth,height;
//需要绘制的Bitmap
privateBitmapbitmap;
/**
*需要绘制的图片的区域
*/
privateRectsrcRect;
/**
*绘制的区域
*/
privateRectFdstRectF;
/**
*画笔
*/
privatePaintpaint;
/**
*是否需要滑动
*/
privatebooleanisNeedSlide;
/**
*已经滑动过的距离
*/
privatefloatslideLength;
/**
*绘制的Bitmap
*/
privateBitmapdrawBitmap;
{
srcRect=newRect();
dstRectF=newRectF();
paint=newPaint();
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(1.0f);
}
publicLongImageView(Contextcontext){
super(context);
}
publicLongImageView(Contextcontext,@NullableAttributeSetattrs){
super(context,attrs);
}
publicLongImageView(Contextcontext,@NullableAttributeSetattrs,intdefStyleAttr){
super(context,attrs,defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
publicLongImageView(Contextcontext,@NullableAttributeSetattrs,intdefStyleAttr,intdefStyleRes){
super(context,attrs,defStyleAttr,defStyleRes);
}
/**
*设置Bitmap
*
*@parambitmap
*需要绘制的Bitmap
*/
publicvoidsetBitmap(Bitmapbitmap){
this.bitmap=bitmap;
}
@Override
protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
intspecSize=MeasureSpec.getSize(widthMeasureSpec);
width=getPaddingLeft()+getPaddingRight()+specSize;
specSize=MeasureSpec.getSize(heightMeasureSpec);
height=getPaddingTop()+getPaddingBottom()+specSize;
if(drawBitmap==null){
drawBitmap=resizeImage(bitmap,width);
if(drawBitmap.getHeight()>1.5*height){
//需要滑动
setNeedSlide(true);
}else{
//不需要滑动
setNeedSlide(false);
srcRect.left=0;
srcRect.top=0;
srcRect.right=drawBitmap.getWidth();
srcRect.bottom=drawBitmap.getHeight();
if(drawBitmap.getHeight()>height){
drawBitmap=resizeImageH(drawBitmap,height-20);
}else{
floatspace=(height-drawBitmap.getHeight());
dstRectF.left=0;
dstRectF.top=space;
dstRectF.right=width;
dstRectF.bottom=height-space;
}
}
}
setMeasuredDimension(width,height);
}
@Override
protectedvoidonDraw(Canvascanvas){
canvas.drawBitmap(drawBitmap,(width-drawBitmap.getWidth())/2,slideLength,paint);
}
/**
*设置是否需要滑动
*
*@paramneedSlide
*trueorfalse
*/
publicvoidsetNeedSlide(booleanneedSlide){
isNeedSlide=needSlide;
}
/**
*触摸操作的坐标
*/
privatefloatlastX;
privatefloatlastY;
@Override
publicbooleanonTouchEvent(MotionEventevent){
if(!isNeedSlide){
returnsuper.onTouchEvent(event);
}
intaction=event.getAction();
switch(action){
caseMotionEvent.ACTION_DOWN:
//按下
lastX=event.getX();
lastY=event.getY();
break;
caseMotionEvent.ACTION_MOVE:
floatmoveX=event.getX();
if(moveX-lastX>50){
//判断为左右滑动
returnsuper.onTouchEvent(event);
}
floatmoveY=event.getY();
floatdistance=moveY-lastY;
lastY=moveY;
slideLength+=distance;
if(slideLength>=0){
slideLength=0;
}
if(slideLength<=(-1)*(drawBitmap.getHeight()-height)){
slideLength=(-1)*(drawBitmap.getHeight()-height);
}
postInvalidate();
break;
default:
break;
}
returntrue;
}
publicBitmapresizeImage(Bitmapbitmap,intw){
intwidth=bitmap.getWidth();
intheight=bitmap.getHeight();
floatscaleWidth=((float)w)/width;
Matrixmatrix=newMatrix();
matrix.postScale(scaleWidth,scaleWidth);
returnBitmap.createBitmap(bitmap,0,0,width,
height,matrix,true);
}
publicBitmapresizeImageH(Bitmapbitmap,inth){
intwidth=bitmap.getWidth();
intheight=bitmap.getHeight();
floatscaleWidth=((float)h)/height;
Matrixmatrix=newMatrix();
matrix.postScale(scaleWidth,scaleWidth);
returnBitmap.createBitmap(bitmap,0,0,width,
height,matrix,true);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。