Android实现网络加载图片点击大图后浏览可缩放

本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下

找了一些demo感觉没有自己想要的效果,于是借鉴一些改造一下并记录下来;

1、主Activity

public class PictureViewFra extends Activity {
  private PicGallery gallery;
  // private ViewGroup tweetLayout; // 弹层
  private boolean mTweetShow = false; // 弹层是否显示 

  // 屏幕宽度
  public static int screenWidth;
  // 屏幕高度
  public static int screenHeight; 

  private GalleryAdapter mAdapter; 

  private ArrayList<HelpTopicImageBean>  helpTopicImage = new ArrayList<HelpTopicImageBean>();
  private int position = 0;
  // private ProgressDialog mProgress; 

  private TextView tv_img_count,tv_topic_title; 

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.picture_view); 

    getIntentData(); 

    tv_img_count = (TextView)findViewById(R.id.tv_img_count);
    tv_topic_title = (TextView)findViewById(R.id.tv_topic_title); 

    gallery = (PicGallery) findViewById(R.id.pic_gallery);
    gallery.setVerticalFadingEdgeEnabled(false);// 取消竖直渐变边框
    gallery.setHorizontalFadingEdgeEnabled(false);// 取消水平渐变边框
    gallery.setDetector(new GestureDetector(this,
        new MySimpleGesture()));
    mAdapter = new GalleryAdapter(this,helpTopicImage,position);
    gallery.setAdapter(mAdapter);
    gallery.setOnItemLongClickListener(new OnItemLongClickListener() {
      @Override
      public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        return false;
      }
    }); 

    mAdapter.getPositionListener(new GalleryAdapter.GalleryPositionListener() {
      @Override
      public void movePosition(int index) {
        Log.d("helpTopicImage--> ", " " + index);
        tv_img_count.setText((index+1)+"/"+helpTopicImage.size());
        tv_topic_title.setText(helpTopicImage.get(index).getImgInfo());
      }
    }); 

//    mAdapter.setData(dataResult);
    initViews();
    // mProgress = ProgressDialog.show(getActivity(),
    // null,getActivity().getString(R.string.loading));
  } 

  private void getIntentData() {
    Intent intent = getIntent();
    helpTopicImage = (ArrayList<HelpTopicImageBean>)intent.getSerializableExtra("helpTopicImage");
    position = intent.getIntExtra("position", 0);
  } 

  private void initViews() { 

    screenWidth = getWindow().getWindowManager().getDefaultDisplay()
        .getWidth();
    screenHeight = getWindow().getWindowManager().getDefaultDisplay()
        .getHeight(); 

  } 

  private class MySimpleGesture extends SimpleOnGestureListener {
    // 按两下的第二下Touch down时触发
    public boolean onDoubleTap(MotionEvent e) { 

      View view = gallery.getSelectedView();
      if (view instanceof MyImageView) {
        MyImageView imageView = (MyImageView) view;
        if (imageView.getScale() > imageView.getMiniZoom()) {
          imageView.zoomTo(imageView.getMiniZoom());
        } else {
          imageView.zoomTo(imageView.getMaxZoom());
        } 

      } else { 

      }
      return true;
    } 

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
      // Logger.LOG("onSingleTapConfirmed",
      // "onSingleTapConfirmed excute");
      // mTweetShow = !mTweetShow;
      // tweetLayout.setVisibility(mTweetShow ? View.VISIBLE
      // : View.INVISIBLE);
      return true;
    }
  } 

}

2、显示图片的GalleryAdapter

public class GalleryAdapter extends BaseAdapter { 

  private Context context; 

  private ArrayList<MyImageView> imageViews = new ArrayList<MyImageView>(); 

  private GalleryPositionListener positionListener; 

  private ArrayList<HelpTopicImageBean>  helpTopicImage = new ArrayList<HelpTopicImageBean>();
  private int position = 0;
  private ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(); 

  private Handler handler = new Handler() {
    public void handleMessage(Message msg) {
      Bitmap bitmap = (Bitmap) msg.obj;
      Bundle bundle = msg.getData();
      String url = bundle.getString("url");
      for (int i = 0; i < imageViews.size(); i++) {
        if (imageViews.get(i).getTag().equals(url)) {
          imageViews.get(i).setImageBitmap(bitmap);
        }
      }
    }
  }; 

  public void setData(List<Integer> data) {
    notifyDataSetChanged();
  } 

  public GalleryAdapter(Context context,ArrayList<HelpTopicImageBean>  helpTopicImage,int position) {
    this.context = context;
    this.helpTopicImage = helpTopicImage;
    this.position = position;
    for(int i=0; i<helpTopicImage.size(); i++){
      Bitmap bitmap = ImageLoader.getInstance().loadImageSync(helpTopicImage.get(i).getImgUrl());
      bitmaps.add(bitmap);
    }
  } 

  @Override
  public int getCount() {
    return helpTopicImage.size();
  } 

  @Override
  public Object getItem(int position) {
    return position;
  } 

  @Override
  public long getItemId(int position) {
    return position;
  } 

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    MyImageView view = new MyImageView(context);
    view.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT,
        LayoutParams.FILL_PARENT)); 

    if (bitmaps.get(position) != null) {
      view.setImageBitmap(bitmaps.get(position));
    }
    if (!this.imageViews.contains(view)) {
      imageViews.add(view);
    }
    positionListener.movePosition(position);
    return view;
  } 

   public void getPositionListener(GalleryPositionListener positionListener) {
      this.positionListener = positionListener;
   } 

  public interface GalleryPositionListener{
      public void movePosition(int index);
  }
}

3、自定义的 Gallery

public class PicGallery extends Gallery {
  private GestureDetector gestureScanner;
  private MyImageView imageView; 

  public PicGallery(Context context) {
    super(context); 

  } 

  public PicGallery(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  } 

  public void setDetector(GestureDetector dectector) {
    gestureScanner = dectector;
  } 

  public PicGallery(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.setOnTouchListener(new OnTouchListener() { 

      float baseValue;
      float originalScale; 

      @Override
      public boolean onTouch(View v, MotionEvent event) {
        View view = PicGallery.this.getSelectedView();
        if (view instanceof MyImageView) {
          imageView = (MyImageView) view; 

          if (event.getAction() == MotionEvent.ACTION_DOWN) {
            baseValue = 0;
            originalScale = imageView.getScale();
          }
          if (event.getAction() == MotionEvent.ACTION_MOVE) {
            if (event.getPointerCount() == 2) {
              float x = event.getX(0) - event.getX(1);
              float y = event.getY(0) - event.getY(1);
              float value = (float) Math.sqrt(x * x + y * y);// 计算两点的距离
              // System.out.println("value:" + value);
              if (baseValue == 0) {
                baseValue = value;
              } else {
                float scale = value / baseValue;// 当前两点间的距离除以手指落下时两点间的距离就是需要缩放的比例。
                // scale the image
                imageView.zoomTo(originalScale * scale, x
                    + event.getX(1), y + event.getY(1)); 

              }
            }
          }
        }
        return false;
      } 

    });
  } 

  float v[] = new float[9]; 

  @Override
  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
      float distanceY) {
    View view = PicGallery.this.getSelectedView();
    if (view instanceof MyImageView) { 

      float xdistance = calXdistance(e1, e2);
      float min_distance = PictureViewFra.screenWidth / 4f; 

      if (isScrollingLeft(e1, e2) && xdistance > min_distance) {
        kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
      } else if (!isScrollingLeft(e1, e2) && xdistance > min_distance) {
        kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
      } 

      imageView = (MyImageView) view; 

      Matrix m = imageView.getImageMatrix();
      m.getValues(v);
      // 图片实时的上下左右坐标
      float left, right;
      // 图片的实时宽,高
      float width = imageView.getScale() * imageView.getImageWidth();
      float height = imageView.getScale() * imageView.getImageHeight(); 

      if ((int) width <= PictureViewFra.screenWidth
          && (int) height <= PictureViewFra.screenHeight)// 如果图片当前大小<屏幕大小,直接处理滑屏事件
      {
        super.onScroll(e1, e2, distanceX, distanceY);
      } else {
        left = v[Matrix.MTRANS_X];
        right = left + width;
        Rect r = new Rect();
        imageView.getGlobalVisibleRect(r); 

        if (distanceX > 0)// 向左滑动
        {
          if (r.left > 0 || right < PictureViewFra.screenWidth) {// 判断当前ImageView是否显示完全
            super.onScroll(e1, e2, distanceX, distanceY);
          } else {
            imageView.postTranslate(-distanceX, -distanceY);
          }
        } else if (distanceX < 0)// 向右滑动
        {
          if (r.right < PictureViewFra.screenWidth || left > 0) {
            super.onScroll(e1, e2, distanceX, distanceY);
          } else {
            imageView.postTranslate(-distanceX, -distanceY);
          }
        } 

      } 

    } else {
      super.onScroll(e1, e2, distanceX, distanceY);
    }
    return false;
  } 

  private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
    return e2.getX() > e1.getX();
  } 

  private float calXdistance(MotionEvent e1, MotionEvent e2) {
    return Math.abs(e2.getX() - e1.getX());
  } 

  @Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
      float velocityY) {
    return false;
  } 

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    //Logger.d(DEBUG,"[PicGallery.onTouchEvent]"+"PicGallery.onTouchEvent");
    if (gestureScanner != null) {
      gestureScanner.onTouchEvent(event);
    }
    switch (event.getAction()) {
    case MotionEvent.ACTION_UP:
      // 判断边界是否越界
      View view = PicGallery.this.getSelectedView();
      if (view instanceof MyImageView) { 

        if(kEvent != KEY_INVALID) { // 是否切换上一页或下一页
          onKeyDown(kEvent, null);
          kEvent = KEY_INVALID;
        } 

        imageView = (MyImageView) view;
        float width = imageView.getScale() * imageView.getImageWidth();
        float height = imageView.getScale()
            * imageView.getImageHeight();
        // Logger.LOG("onTouchEvent", "width=" + width + ",height="
        // + height + ",screenWidth="
        // + PictureViewActivity.screenWidth + ",screenHeight="
        // + PictureViewActivity.screenHeight);
        if ((int) width <= PictureViewFra.screenWidth
            && (int) height <= PictureViewFra.screenHeight)// 如果图片当前大小<屏幕大小,判断边界
        {
          break;
        }
        float v[] = new float[9];
        Matrix m = imageView.getImageMatrix();
        m.getValues(v);
        float top = v[Matrix.MTRANS_Y];
        float bottom = top + height;
        if (top < 0 && bottom < PictureViewFra.screenHeight) {
//         imageView.postTranslateDur(-top, 200f);
          imageView.postTranslateDur(PictureViewFra.screenHeight
              - bottom, 200f);
        }
        if (top > 0 && bottom > PictureViewFra.screenHeight) {
//         imageView.postTranslateDur(PictureViewActivity.screenHeight
//             - bottom, 200f);
          imageView.postTranslateDur(-top, 200f);
        } 

        float left =v[Matrix.MTRANS_X];
        float right = left + width;
        if(left<0 && right< PictureViewFra.screenWidth){
//         imageView.postTranslateXDur(-left, 200f);
          imageView.postTranslateXDur(PictureViewFra.screenWidth
              - right, 200f);
        }
        if(left>0 && right>PictureViewFra.screenWidth){
//         imageView.postTranslateXDur(PictureViewActivity.screenWidth
//             - right, 200f);
          imageView.postTranslateXDur(-left, 200f);
        }
      }
      break;
    }
    return super.onTouchEvent(event);
  } 

  int kEvent = KEY_INVALID; //invalid
  public static final int KEY_INVALID = -1;
}

4、自定义的ImageView

public class MyImageView extends ImageView {
  // This is the base transformation which is used to show the image
  // initially. The current computation for this shows the image in
  // it's entirety, letterboxing as needed. One could choose to
  // show the image as cropped instead.
  //
  // This matrix is recomputed when we go from the thumbnail image to
  // the full size image.
  protected Matrix mBaseMatrix = new Matrix(); 

  // This is the supplementary transformation which reflects what
  // the user has done in terms of zooming and panning.
  //
  // This matrix remains the same when we go from the thumbnail image
  // to the full size image.
  protected Matrix mSuppMatrix = new Matrix(); 

  // This is the final matrix which is computed as the concatentation
  // of the base matrix and the supplementary matrix.
  private final Matrix mDisplayMatrix = new Matrix(); 

  // Temporary buffer used for getting the values out of a matrix.
  private final float[] mMatrixValues = new float[9]; 

  // The current bitmap being displayed.
  protected Bitmap image = null; 

  protected Handler mHandler = new Handler(); 

  int mThisWidth = -1, mThisHeight = -1;//布局后的宽度和高度,由于是全屏显示,这两个值等于屏幕分辨率 

  float mMaxZoom;// 最大缩放比例
  float mMinZoom;// 最小缩放比例 

  private int imageWidth;// 图片的原始宽度
  private int imageHeight;// 图片的原始高度 

  // float scaleRate;// 图片适应屏幕的缩放比例 

  static final float SCALE_RATE = 1.25F; 

  public MyImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  } 

  public MyImageView(Context context) {
    super(context);
    init();
  } 

  @Override
  public void setImageBitmap(Bitmap bitmap) {
    super.setImageBitmap(bitmap);
    image = bitmap;
    setImageHeight(bitmap.getHeight());
    setImageWidth(bitmap.getWidth());
    Drawable d = getDrawable();
    if (d != null) {
      d.setDither(true);
    }
  } 

  // Center as much as possible in one or both axis. Centering is
  // defined as follows: if the image is scaled down below the
  // view's dimensions then center it (literally). If the image
  // is scaled larger than the view and is translated out of view
  // then translate it back into view (i.e. eliminate black bars).
  protected void center(boolean horizontal, boolean vertical) {
    if (image == null) {
      return;
    } 

    Matrix m = getImageViewMatrix(); 

    RectF rect = new RectF(0, 0, image.getWidth(), image.getHeight());
    // RectF rect = new RectF(0, 0, imageWidth*getScale(),
    // imageHeight*getScale()); 

    m.mapRect(rect); 

    float height = rect.height();
    float width = rect.width(); 

    float deltaX = 0, deltaY = 0; 

    if (vertical) {
      int viewHeight = getHeight();
      if (height < viewHeight) {
        deltaY = (viewHeight - height) / 2 - rect.top;
      } else if (rect.top > 0) {
        deltaY = -rect.top;
      } else if (rect.bottom < viewHeight) {
        deltaY = getHeight() - rect.bottom;
      }
    } 

    if (horizontal) {
      int viewWidth = getWidth();
      if (width < viewWidth) {
        deltaX = (viewWidth - width) / 2 - rect.left;
      } else if (rect.left > 0) {
        deltaX = -rect.left;
      } else if (rect.right < viewWidth) {
        deltaX = viewWidth - rect.right;
      }
    } 

    postTranslate(deltaX, deltaY);
    setImageMatrix(getImageViewMatrix());
  } 

  private void init() {
    setScaleType(ScaleType.MATRIX);
  } 

  // Setup the base matrix so that the image is centered and scaled properly.
  private void getProperBaseMatrix(Bitmap bitmap, Matrix matrix) {
    float viewWidth = getWidth();
    float viewHeight = getHeight(); 

    float w = bitmap.getWidth();
    float h = bitmap.getHeight();
    matrix.reset(); 

    // We limit up-scaling to 3x otherwise the result may look bad if it's
    // a small icon.
    float scale = Math.min(viewWidth / w, viewHeight / h); 

    mMinZoom = scale;
    mMaxZoom = 2*scale; 

    matrix.postScale(scale, scale); 

    matrix.postTranslate((viewWidth - w * scale) / 2F, (viewHeight - h
        * scale) / 2F);
  } 

  protected float getValue(Matrix matrix, int whichValue) {
    matrix.getValues(mMatrixValues);
    return mMatrixValues[whichValue];
  } 

  // Get the scale factor out of the matrix.
  protected float getScale(Matrix matrix) {
    return getValue(matrix, Matrix.MSCALE_X);
  } 

  public float getScale() {
    return getScale(mSuppMatrix)*mMinZoom;
  } 

  public float getScaleRate() {
    return getScale(mSuppMatrix);
  } 

  public float getMiniZoom() {
    return mMinZoom;
  } 

  public float getMaxZoom() {
    return mMaxZoom;
  } 

  // Combine the base matrix and the supp matrix to make the final matrix.
  protected Matrix getImageViewMatrix() {
    // The final matrix is computed as the concatentation of the base matrix
    // and the supplementary matrix.
    mDisplayMatrix.set(mBaseMatrix);
    mDisplayMatrix.postConcat(mSuppMatrix);
    return mDisplayMatrix;
  } 

  @Override
  protected void onLayout(boolean changed, int left, int top, int right,
      int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    mThisWidth = right - left;
    mThisHeight = bottom - top;
    if (image != null) {
      getProperBaseMatrix(image, mBaseMatrix);
       setImageMatrix(getImageViewMatrix());
    }
  } 

  protected void zoomTo(float scale, float centerX, float centerY) {
    if (scale > mMaxZoom) {
      scale = mMaxZoom;
    } else if (scale < mMinZoom) {
      scale = mMinZoom;
    } 

    float oldScale = getScale();
    float deltaScale = scale / oldScale; 

    mSuppMatrix.postScale(deltaScale, deltaScale, centerX, centerY);
    setImageMatrix(getImageViewMatrix());
    center(true, true);
  } 

  protected void zoomTo(final float scale, final float centerX,
      final float centerY, final float durationMs) {
    final float incrementPerMs = (scale - getScale()) / durationMs;
    final float oldScale = getScale();
    final long startTime = System.currentTimeMillis(); 

    mHandler.post(new Runnable() {
      public void run() {
        long now = System.currentTimeMillis();
        float currentMs = Math.min(durationMs, now - startTime);
        float target = oldScale + (incrementPerMs * currentMs);
        zoomTo(target, centerX, centerY);
        if (currentMs < durationMs) {
          mHandler.post(this);
        }
      }
    });
  } 

  public void zoomTo(float scale) {
    float cx = getWidth() / 2F;
    float cy = getHeight() / 2F; 

    zoomTo(scale, cx, cy);
  } 

  protected void zoomToPoint(float scale, float pointX, float pointY) {
    float cx = getWidth() / 2F;
    float cy = getHeight() / 2F; 

    panBy(cx - pointX, cy - pointY);
    zoomTo(scale, cx, cy);
  } 

  protected void zoomIn() {
    zoomIn(SCALE_RATE);
  } 

  protected void zoomOut() {
    zoomOut(SCALE_RATE);
  } 

  protected void zoomIn(float rate) {
    if (getScale() >= mMaxZoom) {
      return; // Don't let the user zoom into the molecular level.
    }
    if (image == null) {
      return;
    } 

    float cx = getWidth() / 2F;
    float cy = getHeight() / 2F; 

    mSuppMatrix.postScale(rate, rate, cx, cy);
    setImageMatrix(getImageViewMatrix());
  } 

  protected void zoomOut(float rate) {
    if (image == null) {
      return;
    } 

    float cx = getWidth() / 2F;
    float cy = getHeight() / 2F; 

    // Zoom out to at most 1x.
    Matrix tmp = new Matrix(mSuppMatrix);
    tmp.postScale(1F / rate, 1F / rate, cx, cy); 

    if (getScale(tmp) < 1F) {
      mSuppMatrix.setScale(1F, 1F, cx, cy);
    } else {
      mSuppMatrix.postScale(1F / rate, 1F / rate, cx, cy);
    }
    setImageMatrix(getImageViewMatrix());
    center(true, true);
  } 

  public void postTranslate(float dx, float dy) {
    mSuppMatrix.postTranslate(dx, dy);
    setImageMatrix(getImageViewMatrix());
  } 

  float _dy = 0.0f; 

  protected void postTranslateDur(final float dy, final float durationMs) {
    _dy = 0.0f;
    final float incrementPerMs = dy / durationMs;
    final long startTime = System.currentTimeMillis();
    mHandler.post(new Runnable() {
      public void run() {
        long now = System.currentTimeMillis();
        float currentMs = Math.min(durationMs, now - startTime); 

        postTranslate(0, incrementPerMs * currentMs - _dy);
        _dy = incrementPerMs * currentMs; 

        if (currentMs < durationMs) {
          mHandler.post(this);
        }
      }
    });
  } 

  float _dx = 0.0f; 

  protected void postTranslateXDur(final float dx, final float durationMs) {
    _dx = 0.0f;
    final float incrementPerMs = dx / durationMs;
    final long startTime = System.currentTimeMillis();
    mHandler.post(new Runnable() {
      public void run() {
        long now = System.currentTimeMillis();
        float currentMs = Math.min(durationMs, now - startTime); 

        postTranslate(incrementPerMs * currentMs - _dx, 0);
        _dx = incrementPerMs * currentMs; 

        if (currentMs < durationMs) {
          mHandler.post(this);
        }
      }
    });
  } 

  protected void panBy(float dx, float dy) {
    postTranslate(dx, dy);
    setImageMatrix(getImageViewMatrix());
  } 

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
      event.startTracking();
      return true;
    }
    return super.onKeyDown(keyCode, event);
  } 

  @Override
  public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
        && !event.isCanceled()) {
      if (getScale() > mMinZoom) {
        zoomTo(mMinZoom);
        return true;
      }
    }
    return super.onKeyUp(keyCode, event);
  } 

  public int getImageWidth() {
    return imageWidth;
  } 

  public void setImageWidth(int imageWidth) {
    this.imageWidth = imageWidth;
  } 

  public int getImageHeight() {
    return imageHeight;
  } 

  public void setImageHeight(int imageHeight) {
    this.imageHeight = imageHeight;
  } 

} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android关于Glide的使用(高斯模糊、加载监听、圆角图片)

    高斯模糊.加载监听.圆角图片这些相信大家都很熟悉,那如何实现这些效果,请大家参考本文进行学习. 1.引用 compile 'com.github.bumptech.glide:glide:3.7.0' 2.加载图片 2.1 基本加载 Glide.with(context)     .load(url)     .into(imageView); 2.2 设置加载中和加载失败的情况 Glide.with(context) .load(url) .placeholder(R.drawable.loa

  • android 大图片拖拽并缩放实现原理

    由于最近项目忙,博客一直没有时间更新,今天有点时间就连续更新两篇吧,过过瘾. 这篇图片拖拽缩放也是我在项目中用到的,今天整理一下,将源码奉献给大家,希望对大家以后碰到相似的问题有帮助. 这篇就不做过多介绍了,直接上源码: 复制代码 代码如下: public class SpacePageActivity extends Activity { private LinearLayout linnerLayout_spacepage; private RelativeLayout relativeLa

  • android异步加载图片并缓存到本地实现方法

    在android项目中访问网络图片是非常普遍性的事情,如果我们每次请求都要访问网络来获取图片,会非常耗费流量,而且图片占用内存空间也比较大,图片过多且不释放的话很容易造成内存溢出.针对上面遇到的两个问题,首先耗费流量我们可以将图片第一次加载上面缓存到本地,以后如果本地有就直接从本地加载.图片过多造成内存溢出,这个是最不容易解决的,要想一些好的缓存策略,比如大图片使用LRU缓存策略或懒加载缓存策略.今天首先介绍一下本地缓存图片. 首先看一下异步加载缓存本地代码: 复制代码 代码如下: public

  • Android实现加载广告图片和倒计时的开屏布局

    这是一个android开屏布局的实例,可以用于加载广告图片和倒计时的布局.程序中设置的LayoutParams,划分额外空间比例为6分之5,具体权重比例可根据用户自己需求来自定义,异步加载广告图片,相关的Android代码. 具体实现代码如下: package cn.waps.extend; import android.app.Activity; import android.content.Context; import android.content.res.Configuration;

  • Android 图片缩放与旋转的实现详解

    本文使用Matrix实现Android实现图片缩放与旋转.示例代码如下: 复制代码 代码如下: package com.android.matrix;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Matrix;import android.graphics.drawable.BitmapDrawable

  • Android中利用matrix 控制图片的旋转、缩放、移动

    本文主要讲解利用android中Matrix控制图形的旋转缩放移动,具体参见一下代码: 复制代码 代码如下: /**  * 使用矩阵控制图片移动.缩放.旋转  */  public class CommonImgEffectView extends View { private Context context ;      private Bitmap mainBmp , controlBmp ;      private int mainBmpWidth , mainBmpHeight , c

  • Android加载大分辨率图片到手机内存中的实例方法

    还原堆内存溢出的错误首先来还原一下堆内存溢出的错误.首先在SD卡上放一张照片,分辨率为(3776 X 2520),大小为3.88MB,是我自己用相机拍的一张照片.应用的布局很简单,一个Button一个ImageView,然后按照常规的方式,使用BitmapFactory加载一张照片并使用一个ImageView展示. 代码如下: 复制代码 代码如下: btn_loadimage.setOnClickListener(new View.OnClickListener() { @Override   

  • Android中Glide加载库的图片缓存配置究极指南

    零.选择Glide 为什么图片加载我首先推荐Glide? 图片加载框架用了不少,从afinal框架的afinalBitmap,Xutils的BitmapUtils,老牌框架universalImageLoader,著名开源组织square的picasso,google推荐的glide到FaceBook推出的fresco.这些我前前后后都体验过,那么面对这么多的框架,该如何选择呢?下面简单分析下我的看法. afinal和Xuils在github上作者已经停止维护了,开源社区最新的框架要属KJFra

  • android 多点触摸图片缩放的具体实现方法

    布局: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/relativeLayout1"    android:layout_width="fill_parent

  • Android中ListView异步加载图片错位、重复、闪烁问题分析及解决方案

    Android ListView异步加载图片错位.重复.闪烁分析以及解决方案,具体问题分析以及解决方案请看下文. 我们在使用ListView异步加载图片的时候,在快速滑动或者网络不好的情况下,会出现图片错位.重复.闪烁等问题,其实这些问题总结起来就是一个问题,我们需要对这些问题进行ListView的优化. 比如ListView上有100个Item,一屏只显示10个Item,我们知道getView()中convertView是用来复用View对象的,因为一个Item的对应一个View对象,而Ima

随机推荐