效果图如下图所示:

写入代码:

1.首先创建Activity

        
  1. publicclassGalleryMainextendsActivityimplementsOnItemClickListener
  2. {
  3. privateViewScrolldetail;
  4. privateImageAdapteria;
  5. privateLinearLayoutll;
  6. privateLinearLayout.LayoutParamsparm;
  7. privateGalleryg;
  8. @Override
  9. protectedvoidonCreate(BundlesavedInstanceState)
  10. {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.main);
  13. g=(Gallery)findViewById(R.id.myggg);
  14. ll=(LinearLayout)findViewById(R.id.twill);
  15. parm=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
  16. ia=newImageAdapter(this);
  17. detail=newViewScroll(GalleryMain.this,ia.imgIds[0],g);
  18. ll.addView(detail,parm);
  19. g.setAdapter(ia);
  20. g.setOnItemClickListener(this);
  21. }
  22. @Override
  23. publicvoidonItemClick(AdapterView<?>arg0,Viewarg1,intarg2,longarg3)
  24. {
  25. ll.removeView(detail);
  26. detail=newViewScroll(GalleryMain.this,ia.imgIds[arg2],g);
  27. ll.addView(detail,parm);
  28. }
  29. }

main.xml

        
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_height="fill_parent"
  5. android:layout_width="fill_parent"android:gravity="top"android:id="@+id/twill"
  6. android:background="@android:color/transparent">
  7. <Gallery
  8. android:id="@+id/myggg"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. />
  12. </LinearLayout>

2. 可移动的ImageView 需要重写ImageView

        
  1. publicclassTouchViewextendsImageView{
  2. staticfinalintNONE=0;
  3. staticfinalintDRAG=1;//拖动中
  4. staticfinalintZOOM=2;//缩放中
  5. staticfinalintBIGGER=3;//放大ing
  6. staticfinalintSMALLER=4;//缩小ing
  7. privateintmode=NONE;//当前的事件
  8. privatefloatbeforeLenght;//两触点距离
  9. privatefloatafterLenght;//两触点距离
  10. privatefloatscale=0.04f;//缩放的比例XY方向都是这个值越大缩放的越快
  11. privateintscreenW;
  12. privateintscreenH;
  13. /*处理拖动变量*/
  14. privateintstart_x;
  15. privateintstart_y;
  16. privateintstop_x;
  17. privateintstop_y;
  18. privateTranslateAnimationtrans;//处理超出边界的动画
  19. publicTouchView(Contextcontext,intw,inth){
  20. super(context);
  21. this.setPadding(0,0,0,0);
  22. screenW=w;
  23. screenH=h;
  24. }
  25. floatglowX=30;
  26. floatglowY=30;
  27. floatradius=20;
  28. @Override
  29. publicvoiddraw(Canvascanvas){
  30. super.draw(canvas);
  31. if(drawGlow)
  32. canvas.drawCircle(glowX,glowY,radius,paint);
  33. }
  34. Paintpaint=newPaint();
  35. {
  36. paint.setAntiAlias(true);
  37. paint.setColor(Color.RED);
  38. paint.setAlpha(100);
  39. };
  40. booleandrawGlow=false;
  41. /**
  42. *就算两点间的距离
  43. */
  44. privatefloatspacing(MotionEventevent){
  45. floatx=event.getX(0)-event.getX(1);
  46. floaty=event.getY(0)-event.getY(1);
  47. returnFloatMath.sqrt(x*x+y*y);
  48. }
  49. /**
  50. *处理触碰..
  51. */
  52. @Override
  53. publicbooleanonTouchEvent(MotionEventevent){
  54. switch(event.getAction()&MotionEvent.ACTION_MASK){
  55. caseMotionEvent.ACTION_DOWN:
  56. drawGlow=true;
  57. mode=DRAG;
  58. stop_x=(int)event.getRawX();
  59. stop_y=(int)event.getRawY();
  60. start_x=(int)event.getX();
  61. start_y=stop_y-this.getTop();
  62. if(event.getPointerCount()==2)
  63. beforeLenght=spacing(event);
  64. break;
  65. caseMotionEvent.ACTION_POINTER_DOWN:
  66. if(spacing(event)>10f){
  67. mode=ZOOM;
  68. beforeLenght=spacing(event);
  69. }
  70. break;
  71. caseMotionEvent.ACTION_UP:
  72. /*判断是否超出范围并处理*/
  73. drawGlow=false;
  74. intdisX=0;
  75. intdisY=0;
  76. if(getHeight()<=screenH||this.getTop()<0){
  77. if(this.getTop()<0){
  78. intdis=getTop();
  79. this.layout(this.getLeft(),0,this.getRight(),
  80. 0+this.getHeight());
  81. disY=dis-getTop();
  82. }elseif(this.getBottom()>screenH){
  83. disY=getHeight()-screenH+getTop();
  84. this.layout(this.getLeft(),screenH-getHeight(),
  85. this.getRight(),screenH);
  86. }
  87. }
  88. if(getWidth()<=screenW){
  89. if(this.getLeft()<0){
  90. disX=getLeft();
  91. this.layout(0,this.getTop(),0+getWidth(),
  92. this.getBottom());
  93. }elseif(this.getRight()>screenW){
  94. disX=getWidth()-screenW+getLeft();
  95. this.layout(screenW-getWidth(),this.getTop(),screenW,
  96. this.getBottom());
  97. }
  98. }
  99. if(disX!=0||disY!=0){
  100. trans=newTranslateAnimation(disX,0,disY,0);
  101. trans.setDuration(500);
  102. this.startAnimation(trans);
  103. }
  104. mode=NONE;
  105. break;
  106. caseMotionEvent.ACTION_POINTER_UP:
  107. mode=NONE;
  108. break;
  109. caseMotionEvent.ACTION_MOVE:
  110. /*处理拖动*/
  111. if(mode==DRAG){
  112. if(Math.abs(stop_x-start_x-getLeft())<88
  113. &&Math.abs(stop_y-start_y-getTop())<85){
  114. this.setPosition(stop_x-start_x,stop_y-start_y,stop_x
  115. +this.getWidth()-start_x,stop_y-start_y
  116. +this.getHeight());
  117. stop_x=(int)event.getRawX();
  118. stop_y=(int)event.getRawY();
  119. }
  120. }
  121. /*处理缩放*/
  122. elseif(mode==ZOOM){
  123. if(spacing(event)>10f){
  124. afterLenght=spacing(event);
  125. floatgapLenght=afterLenght-beforeLenght;
  126. if(gapLenght==0){
  127. break;
  128. }elseif(Math.abs(gapLenght)>5f){
  129. if(gapLenght>0){
  130. this.setScale(scale,BIGGER);
  131. }else{
  132. this.setScale(scale,SMALLER);
  133. }
  134. beforeLenght=afterLenght;
  135. }
  136. }
  137. }
  138. break;
  139. }
  140. glowX=event.getX();
  141. glowY=event.getY();
  142. this.invalidate();
  143. returntrue;
  144. }
  145. /**
  146. *实现处理缩放
  147. */
  148. privatevoidsetScale(floattemp,intflag){
  149. if(flag==BIGGER){
  150. this.setFrame(this.getLeft()-(int)(temp*this.getWidth()),
  151. this.getTop()-(int)(temp*this.getHeight()),
  152. this.getRight()+(int)(temp*this.getWidth()),
  153. this.getBottom()+(int)(temp*this.getHeight()));
  154. }elseif(flag==SMALLER){
  155. this.setFrame(this.getLeft()+(int)(temp*this.getWidth()),
  156. this.getTop()+(int)(temp*this.getHeight()),
  157. this.getRight()-(int)(temp*this.getWidth()),
  158. this.getBottom()-(int)(temp*this.getHeight()));
  159. }
  160. }
  161. /**
  162. *实现处理拖动
  163. */
  164. privatevoidsetPosition(intleft,inttop,intright,intbottom){
  165. this.layout(left,top,right,bottom);
  166. }
  167. }

3.

        
  1. /**
  2. *一个绝对布局
  3. *@authorAdministrator
  4. *
  5. */
  6. @SuppressWarnings("deprecation")
  7. publicclassViewScrollextendsAbsoluteLayout
  8. {
  9. privateintscreenW;//可用的屏幕宽
  10. privateintscreenH;//可用的屏幕高总高度-上面组件的总高度
  11. privateintimgW;//图片原始宽
  12. privateintimgH;//图片原始高
  13. privateTouchViewtv;
  14. publicViewScroll(Contextcontext,intresId,ViewtopView)
  15. {
  16. super(context);
  17. screenW=((Activity)context).getWindowManager().getDefaultDisplay().getWidth();
  18. screenH=((Activity)context).getWindowManager().getDefaultDisplay().getHeight()-(topView==null?190:topView.getBottom()+50);
  19. tv=newTouchView(context,screenW,screenH);
  20. tv.setImageResource(resId);
  21. Bitmapimg=BitmapFactory.decodeResource(context.getResources(),resId);
  22. imgW=img.getWidth();
  23. imgH=img.getHeight();
  24. intlayout_w=imgW>screenW?screenW:imgW;//实际显示的宽
  25. intlayout_h=imgH>screenH?screenH:imgH;//实际显示的高
  26. if(layout_w==screenW||layout_h==screenH)
  27. tv.setScaleType(ScaleType.FIT_XY);
  28. tv.setLayoutParams(newAbsoluteLayout.LayoutParams(layout_w,layout_h,layout_w==screenW?0:(screenW-layout_w)/2,layout_h==screenH?0:(screenH-layout_h)/2));
  29. this.addView(tv);
  30. }

4.Gallery上的被画上相框的ImageView

        
  1. packagecom.app;
  2. importandroid.content.Context;
  3. importandroid.graphics.Bitmap;
  4. importandroid.graphics.BitmapFactory;
  5. importandroid.graphics.Canvas;
  6. importandroid.graphics.Color;
  7. importandroid.graphics.Matrix;
  8. importandroid.graphics.Paint;
  9. importandroid.graphics.drawable.BitmapDrawable;
  10. importandroid.view.MotionEvent;
  11. importandroid.widget.ImageView;
  12. /**
  13. *ImageAdapter中ImageView的实现类
  14. *@authorAdministrator
  15. *
  16. */
  17. publicclassImageViewImpextendsImageView
  18. {
  19. privateintalpha=250;
  20. privatebooleanpressed=false;
  21. publicImageViewImp(Contextcontext)
  22. {
  23. super(context);
  24. }
  25. publicvoidshow()
  26. {
  27. newThread(){
  28. publicvoidrun(){
  29. inttime=2000;
  30. try
  31. {
  32. pressed=true;
  33. while(time>0)
  34. {
  35. Thread.sleep(200);
  36. time-=200;
  37. alpha-=25;
  38. postInvalidate();
  39. }
  40. pressed=false;
  41. }
  42. catch(Exceptione)
  43. {
  44. e.printStackTrace();
  45. }
  46. };
  47. }.start();
  48. }
  49. @Override
  50. publicbooleanonTouchEvent(MotionEventevent)
  51. {
  52. if(event.getAction()==MotionEvent.ACTION_DOWN)
  53. show();
  54. returnfalse;
  55. }
  56. @Override
  57. protectedvoidonDraw(Canvascanvas)
  58. {
  59. Paintp=newPaint();
  60. p.setColor(Color.WHITE);
  61. p.setStyle(Paint.Style.STROKE);
  62. p.setStrokeWidth(10);
  63. BitmapDrawablebd=(BitmapDrawable)getDrawable();
  64. if(bd!=null)
  65. {
  66. canvas.drawBitmap(imageScale(bd.getBitmap(),107,113),21,18,p);
  67. }
  68. canvas.drawBitmap(BitmapFactory.decodeResource(getContext().getResources(),R.drawable.kua),0,0,p);
  69. if(isPressed())
  70. {
  71. canvas.drawRect(5,5,140,140,p);
  72. }
  73. if(pressed)
  74. {
  75. p.setAlpha(alpha);
  76. canvas.drawRect(5,5,140,140,p);
  77. }
  78. }
  79. publicstaticBitmapimageScale(Bitmapbitmap,intdst_w,intdst_h){
  80. intsrc_w=bitmap.getWidth();
  81. intsrc_h=bitmap.getHeight();
  82. floatscale_w=((float)dst_w)/src_w;
  83. floatscale_h=((float)dst_h)/src_h;
  84. Matrixmatrix=newMatrix();
  85. matrix.postScale(scale_w,scale_h);
  86. Bitmapdstbmp=Bitmap.createBitmap(bitmap,0,0,src_w,src_h,matrix,true);
  87. returndstbmp;
  88. }
  89. }

5.Gallery的适配器

        
  1. packagecom.app;
  2. importandroid.content.Context;
  3. importandroid.view.View;
  4. importandroid.view.ViewGroup;
  5. importandroid.widget.BaseAdapter;
  6. importandroid.widget.Gallery;
  7. importandroid.widget.ImageView;
  8. importandroid.widget.ImageView.ScaleType;
  9. /**
  10. *Gallery的适配器类
  11. *@authorAdministrator
  12. *
  13. */
  14. publicclassImageAdapterextendsBaseAdapter
  15. {
  16. /*图片素材*/
  17. publicint[]imgIds={R.drawable.jpg,R.drawable.pic};
  18. privateContextcontext;
  19. publicImageAdapter(Contextcontext)
  20. {
  21. this.context=context;
  22. }
  23. @Override
  24. publicintgetCount()
  25. {
  26. returnimgIds.length;
  27. }
  28. @Override
  29. publicObjectgetItem(intposition)
  30. {
  31. returnnull;
  32. }
  33. @Override
  34. publiclonggetItemId(intposition)
  35. {
  36. return0;
  37. }
  38. @Override
  39. publicViewgetView(intposition,ViewconvertView,ViewGroupparent)
  40. {
  41. ImageViewimg=newImageViewImp(context);
  42. img.setImageResource(imgIds[position]);
  43. img.setScaleType(ScaleType.CENTER);
  44. img.setLayoutParams(newGallery.LayoutParams(155,150));
  45. returnimg;
  46. }
  47. }

更多相关文章

  1. Android(安卓)图片OutOfMemory异常bitmap size exceeds VM budge
  2. android绘制圆形图片、圆圈以及图片缩放
  3. GestureDetector.OnGestureListener 详解
  4. android 胡言乱语 2 android UI
  5. 网页 Android(安卓)套壳
  6. android 图片的缩放,bitmap的用法
  7. 完整版仿360等手机卫士火箭拖动版
  8. Android(安卓)Matrix源码详解
  9. Android通过webview调起微信和支付宝app进行支付

随机推荐

  1. 服务端开发指南与最佳实战 | 数据存储技
  2. JavaScript测试教程-part 2:引入 Enzyme
  3. 单例模式 - 只有一个实例
  4. 手撕JS(可能持续更新···)
  5. JavaScript 测试教程 part 1:用 Jest 进行
  6. SOA 对比微服务架构
  7. 服务端开发指南与最佳实战 | 数据存储技
  8. vtp基本命令
  9. 原型模式 - 通过复制生成实例
  10. 服务端开发指南与最佳实战 | 数据存储技