您好,登錄后才能下訂單哦!
這篇文章主要介紹了Android如何實現仿微博加載長圖滾動查看效果,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體內容如下
解決步驟
1.將圖片縮放到與控件等寬
2.判斷縮放后的圖片高度,如果高度大于控件高度較多(這里設置的是1.5倍),認定為長圖,可滑動查看圖片
|-如果高度小于控件高度的1.5倍,以控件高度為基準,重新縮放圖片
package org.wandcf_ces.fairproject.widgets; import android.annotation.TargetApi; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.RectF; import android.os.Build; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; /** * Created by sunrui on 2017/3/8. * 加載長圖片 * 解決步驟 * 1.將圖片縮放到與控件等寬 * 2.判斷縮放后的圖片高度,如果高度大于控件高度較多(這里設置的是1.5倍),認定為長圖,可滑動查看圖片 * |-如果高度小于控件高度的1.5倍,以控件高度為基準,重新縮放圖片 * */ public class LongImageView extends View { private int width, height; //需要繪制的Bitmap private Bitmap bitmap; /** * 需要繪制的圖片的區域 */ private Rect srcRect; /** * 繪制的區域 */ private RectF dstRectF; /** * 畫筆 */ private Paint paint; /** * 是否需要滑動 */ private boolean isNeedSlide; /** * 已經滑動過的距離 */ private float slideLength; /** * 繪制的Bitmap */ private Bitmap drawBitmap; { srcRect = new Rect(); dstRectF = new RectF(); paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.BLACK); paint.setStrokeWidth(1.0f); } public LongImageView(Context context) { super(context); } public LongImageView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public LongImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public LongImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } /** * 設置Bitmap * * @param bitmap * 需要繪制的Bitmap */ public void setBitmap(Bitmap bitmap) { this.bitmap = bitmap; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int specSize = 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 { float space = (height - drawBitmap.getHeight()); dstRectF.left = 0; dstRectF.top = space; dstRectF.right = width; dstRectF.bottom = height - space; } } } setMeasuredDimension(width, height); } @Override protected void onDraw(Canvas canvas) { canvas.drawBitmap(drawBitmap, (width - drawBitmap.getWidth()) / 2, slideLength, paint); } /** * 設置是否需要滑動 * * @param needSlide * true or false */ public void setNeedSlide(boolean needSlide) { isNeedSlide = needSlide; } /** * 觸摸操作的坐標 */ private float lastX; private float lastY; @Override public boolean onTouchEvent(MotionEvent event) { if (!isNeedSlide) { return super.onTouchEvent(event); } int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: //按下 lastX = event.getX(); lastY = event.getY(); break; case MotionEvent.ACTION_MOVE: float moveX = event.getX(); if (moveX - lastX > 50) { //判斷為左右滑動 return super.onTouchEvent(event); } float moveY = event.getY(); float distance = 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; } return true; } public Bitmap resizeImage(Bitmap bitmap, int w) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); float scaleWidth = ((float) w) / width; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleWidth); return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); } public Bitmap resizeImageH(Bitmap bitmap, int h) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); float scaleWidth = ((float) h) / height; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleWidth); return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); } }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Android如何實現仿微博加載長圖滾動查看效果”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。