安卓大图加载-自定义view

1.自定义布局,可水平和垂直滚动。

package ccav.xulingyun.com.myapplication.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.io.IOException;

import ccav.xulingyun.com.myapplication.util.HttpUtil;

/**
 * Created by 飞翔的泥巴 on 2017/6/2.
 * 描述:加载大图
 */

public class BigBigPic extends View {

    /**
     * 控件的宽高
     */
    int width;
    int height;
    /**
     * 屏幕的宽高
     */
    int screenWidth;
    int screenHeight;
    /**
     * 图片的宽高
     */
    int picWidth;
    int picHeight;
    /**
     * 按下的坐标
     */
    int downX;
    int downY;
    /**
     * 移动的距离
     */
    int moveX;
    int moveY;
    /**
     * 要绘制的矩形
     */
    Rect mRect;
    /**
     * 大图的解码类
     */
    private BitmapRegionDecoder mBitmapRegionDecoder;
    /**
     * 解码的选项
     */
    BitmapFactory.Options mOptions;

    /**
     * 判断滚动的方向,水平滚动SCROLL_H,垂直滚动SCROLL_V
     */
    public static final int SCROLL_V = 0;
    public static final int SCROLL_H = 1;
    public int scrool_orientation = 0;


    public BigBigPic(Context context) {
        this(context, null);
    }

    public BigBigPic(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BigBigPic(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        getScreenSize();
        mRect = new Rect();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else if (widthMode == MeasureSpec.AT_MOST) {
            width = Math.min(widthSize,picWidth);
        }
        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else if (heightMode == MeasureSpec.AT_MOST) {
            height = Math.min(heightSize,picHeight);
        }
        mRect.left = 0;
        mRect.top = 0;
        mRect.right = width;
        mRect.bottom = height;
        Log.e("TAG", "onMeasure: "+width+","+height+","+screenWidth+","+screenHeight);
        setMeasuredDimension(width, height);
    }

    /**
     * 屏幕的宽高
     */
    private void getScreenSize() {
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm);
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
    }

    /**
     * 设置图片
     */
    public void setImage() {
        try {
            mBitmapRegionDecoder = BitmapRegionDecoder.newInstance(HttpUtil.getOutPut(),0,0,false);
            Thread.sleep(5000);
            deal();
        } catch (Exception $E) {
            $E.printStackTrace();
        }
    }
    public void setAssetsImage(String path) {
        try {
            mBitmapRegionDecoder = BitmapRegionDecoder.newInstance(getResources().getAssets().open(path), false);
            deal();
        } catch (IOException $E) {
            $E.printStackTrace();
        }
    }

    private void deal(){
        picWidth = mBitmapRegionDecoder.getWidth();
        picHeight = mBitmapRegionDecoder.getHeight();
        if(picHeight>screenHeight){
            scrool_orientation = SCROLL_V;
        }else{
            scrool_orientation = SCROLL_H;
        }
        mOptions = new BitmapFactory.Options();
        mOptions.inPreferredConfig = Bitmap.Config.RGB_565;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if(mBitmapRegionDecoder==null)
            return;
        if(scrool_orientation==SCROLL_H){
            canvas.translate(0,screenHeight/2-picHeight/2);
        }else{
            canvas.translate(screenWidth/2-picWidth/2,0);
        }
        canvas.drawBitmap(mBitmapRegionDecoder.decodeRegion(mRect, mOptions), 0, 0, null);
    }


    /**
     * 手势的处理
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) event.getX();
                downY = (int) event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                moveX = (int) (downX-event.getX());
                moveY = (int) (downY-event.getY());
                if(scrool_orientation==SCROLL_H){
                    mRect.left +=moveX;
                    mRect.right += moveX;
                }else{
                    mRect.top +=moveY;
                    mRect.bottom +=moveY;
                }
                checkBound();
                downX = (int) event.getX();
                downY = (int) event.getY();
                invalidate();
                break;
        }
        return true;
    }

    /**
     * 边缘检测
     */
    private void checkBound() {
        if(scrool_orientation==SCROLL_H){
            if(mRect.left<0){
                mRect.left = 0;
                mRect.right = width;
            }
            if(mRect.right>picWidth){
                mRect.left = picWidth-width;
                mRect.right = picWidth;
            }
        }else{
            if(mRect.top<0){
                mRect.top = 0;
                mRect.bottom = height;
            }
            if(mRect.bottom>picHeight){
                mRect.top = picHeight-height;
                mRect.bottom = picHeight;
            }
        }
    }
}

2.在activity里面调用

BigBigPic mImageView = (BigBigPic) findViewById(R.id.bigpic);
mImageView.setAssetsImage("qmsht.jpg");

3.布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ccav.xulingyun.com.myapplication.BigPicActivity">
    <ccav.xulingyun.com.myapplication.view.BigBigPic
        android:background="#000000"
        android:id="@+id/bigpic"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值