本文实例为大家分享了AndroID九宫格图片展示的具体代码,供大家参考,具体内容如下
找了一些demo感觉没有自己想要的效果,于是借鉴一些改造一下并记录下来;
1、主Activity
public class PictureVIEwFra extends Activity { private Picgallery gallery; // private VIEwGroup tweetLayout; // d层 private boolean mTweetShow = false; // d层是否显示 // 屏幕宽度 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).get@R_403_5759@l()); 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,distanceY); } else { imageVIEw.postTranslate(-distanceX,-distanceY); } } else if (distanceX < 0)// 向右滑动 { if (r.right < PictureVIEwFra.screenWIDth || left > 0) { super.onScroll(e1,-distanceY); } } } } else { super.onScroll(e1,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,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,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,image.getWIDth(),image.getHeight()); // RectF rect = new RectF(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,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,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,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,cy); if (getScale(tmp) < 1F) { mSuppMatrix.setScale(1F,1F,cy); } else { mSuppMatrix.postscale(1F / rate,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; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android实现网络加载图片点击大图后浏览可缩放全部内容,希望文章能够帮你解决Android实现网络加载图片点击大图后浏览可缩放所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)