Android应用中图片浏览时实现自动切换功能的方法详解

Android应用中图片浏览时实现自动切换功能的方法详解,第1张

概述先给最终效果图:当我们在最下边的gallery中切换图片时,上面的大图片会自动切换,切换时有动画效果哦,很简单的一个程序,有待完善更多的功能!

先给最终效果图:

当我们在最下边的gallery中切换图片时,上面的大图片会自动切换,切换时有动画效果哦,很简单的一个程序,有待完善更多的功能!

activity代码:

package cn.com.chenzheng_java;  import androID.app.Activity; import androID.content.Context; import androID.os.Bundle; import androID.vIEw.VIEw; import androID.vIEw.VIEwGroup; import androID.vIEw.Window; import androID.vIEw.animation.AnimationUtils; import androID.Widget.AdapterVIEw; import androID.Widget.BaseAdapter; import androID.Widget.gallery; import androID.Widget.ImageSwitcher; import androID.Widget.ImageVIEw; import androID.Widget.Toast; import androID.Widget.AdapterVIEw.OnItemSelectedListener; import androID.Widget.ImageVIEw.ScaleType; import androID.Widget.VIEwSwitcher.VIEwFactory; public class galleryActivity extends Activity implements OnItemSelectedListener {    int[] imagesID = new int[] { R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4    };   int index = R.drawable.a1;   ImageSwitcher imageSwitcher;    @OverrIDe   protected voID onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     /**      * 设置窗口无标题栏,一定要在setContentVIEw前进行设置哦      */     requestwindowFeature(Window.FEATURE_NO_Title);     setContentVIEw(R.layout.gallery);      gallery gallery = (gallery) findVIEwByID(R.ID.gallery1);     gallery.setonItemSelectedListener(this);      imageSwitcher = (ImageSwitcher) findVIEwByID(R.ID.imageSwitcher);     imageSwitcher.setVisibility(VIEw.VISIBLE);          /***      * setInAnimation可以设置淡入动画      * setoutAnimation可以设置淡出动画      */     imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(         getApplicationContext(),androID.R.anim.fade_in));     imageSwitcher.setoutAnimation(AnimationUtils.loadAnimation(         getApplicationContext(),androID.R.anim.fade_out));      imageSwitcher.setFactory(new VIEwFactory() {       /**        * 创建一个新的图片放置到ImageSwitcher上,可以使用其设置背景哦。        * 一般只会在创建时调用一次。相当于为我们创建一个进行动画效果时的一个背景图。        * 此方法亦可以通过setVIEw替代        */       @OverrIDe       public VIEw makeVIEw() {         ImageVIEw imageVIEw = new ImageVIEw(galleryActivity.this);         // 设置截取模式         imageVIEw.setScaleType(ScaleType.CENTER_INSIDE);         imageVIEw.setBackgroundResource(R.drawable.ground);         Toast.makeText(getApplicationContext(),"执行了一次",Toast.LENGTH_SHORT).show();         return imageVIEw;       }     });          gallery.setVisibility(VIEw.VISIBLE);     gallery.setAdapter(new ImageAdapter(this));    }    class ImageAdapter extends BaseAdapter {     private Context context2;      public ImageAdapter(Context context) {       context2 = context;     }      @OverrIDe     public int getCount() {       return imagesID.length;     }      @OverrIDe     public Object getItem(int position) {       return imagesID[position];     }      @OverrIDe     public long getItemID(int position) {       return position;     }      @OverrIDe     public VIEw getVIEw(int position,VIEw image2,VIEwGroup parent) {       ImageVIEw image = new ImageVIEw(context2);       image.setBackgroundResource(imagesID[position]);       /**        * setScaleType()可以设置当图片大小和容器大小不匹配时的剪辑模式.        * ScaleType.CENTER_INSIDE的意思是,按图片的原比例缩小或者扩大,直到长或者宽中的任何一个顶到容器为止        */       image.setScaleType(ScaleType.CENTER_INSIDE);        return image;     }    }   /**    * 当gallery的图像发生变化时,同步ImageSwitcher的变化    */   @OverrIDe   public voID onItemSelected(AdapterVIEw<?> parent,VIEw vIEw,int position,long ID) {     imageSwitcher.setimageResource(imagesID[position]);    }    @OverrIDe   public voID onnothingSelected(AdapterVIEw<?> parent) {    }  } 

gallery.xml

<?xml version="1.0" enCoding="utf-8"?> <linearLayout  xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:layout_wIDth="fill_parent"  androID:layout_height="fill_parent"  androID:orIEntation="vertical"  androID:gravity="center_horizontal"  >    <ImageSwitcher   androID:ID="@+ID/imageSwitcher"   androID:layout_height="350dp"   androID:layout_wIDth="fill_parent"   ></ImageSwitcher>      <gallery androID:ID="@+ID/gallery1"    androID:gravity="center"   androID:layout_wIDth="70dp"    androID:layout_height="100dp"></gallery> </linearLayout> 

升级版

较之上面的,效果还是一样的,只不过这里添加了这么以下几个功能:

第一:下方图片的自适应长宽(在保证长宽比例的情况下哦)

第二:触摸大图同样可以进行图片切换(没有机器,没法测试,但是基本的思路还是正确的)

代码:

activity代码:

package cn.com.chenzheng_java;  import java.text.DecimalFormat;  import androID.app.Activity; import androID.content.Context; import androID.content.res.Resources; import androID.graphics.Bitmap; import androID.graphics.BitmapFactory; import androID.os.Bundle; import androID.util.Log; import androID.vIEw.MotionEvent; import androID.vIEw.VIEw; import androID.vIEw.VIEwGroup; import androID.vIEw.Window; import androID.vIEw.VIEw.OntouchListener; import androID.vIEw.animation.AnimationUtils; import androID.Widget.AdapterVIEw; import androID.Widget.BaseAdapter; import androID.Widget.gallery; import androID.Widget.ImageSwitcher; import androID.Widget.ImageVIEw; import androID.Widget.Toast; import androID.Widget.AdapterVIEw.OnItemSelectedListener; import androID.Widget.gallery.LayoutParams; import androID.Widget.ImageVIEw.ScaleType; import androID.Widget.VIEwSwitcher.VIEwFactory; public class galleryActivity extends Activity implements OnItemSelectedListener,OntouchListener{    int[] imagesID = new int[] { R.drawable.a1,R.drawable.a4    };   int index = 0;   ImageSwitcher imageSwitcher;   int maxImageWIDth = 70;   int maxImageHeight = 100;    @OverrIDe   protected voID onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     /**      * 设置窗口无标题栏,Toast.LENGTH_SHORT).show();         return imageVIEw;       }     });          gallery.setVisibility(VIEw.VISIBLE);     gallery.setAdapter(new ImageAdapter(this));    }    class ImageAdapter extends BaseAdapter {     private Context context2;      public ImageAdapter(Context context) {       context2 = context;     }      @OverrIDe     public int getCount() {       return imagesID.length;     }      @OverrIDe     public Object getItem(int position) {       return imagesID[position];     }      @OverrIDe     public long getItemID(int position) {       return position;     }     /***      * 该方法的实现稍微复杂一些,主要思想就是,要对图片的大小进行自动剪裁,让其正好宽或者长顶到允许的最大值      * 这里主要用到了BitmapFactory Bitmap 用于图片获取和处理      * DecaimalFormat 用于对double数据进行格式规划(这里是只允许小数点后两位)      */     @OverrIDe     public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {       index = position;       ImageVIEw image = new ImageVIEw(context2);       Resources resources = context2.getResources();       // 加载图片为Bitmap位图       Bitmap bitmap = BitmapFactory.decodeResource(resources,imagesID[position]);       Log.i("是否可以decode这张图片",(bitmap!=null)+"");              // 图片的原始大小       int bitMapWIDth = bitmap.getWIDth();       int bitMapHeight = bitmap.getHeight();       Log.i("bitMapWIDth",bitMapWIDth+"");       Log.i("bitMapHeight",bitMapHeight+"");              // 缩小或者放大的倍数关系       double scale;       double wIDthScale = (maxImageWIDth*100)/(bitMapWIDth*100.0);//这里乘以100然后除以100.0的目的是为了让其返回double类型       double heightScale = (maxImageHeight*100)/(bitMapHeight*100.0);              // 规划获得的格式(小数点后两位)       DecimalFormat decimalFormat = new DecimalFormat("#.00");       wIDthScale = Double.valueOf(decimalFormat.format(wIDthScale));       heightScale = Double.valueOf(decimalFormat.format(heightScale));              Log.i("wIDthScale",wIDthScale+"");       Log.i("heightScale",heightScale+"");                     if(wIDthScale>= heightScale){         scale = wIDthScale;       }       else{         scale = heightScale;       }              Log.i("scale",scale+"");              // 根据原图片生成一个缩放后的图片       Bitmap bitmap2 = Bitmap.createScaledBitmap(bitmap,(int)(bitMapWIDth*scale),(int)(bitMapHeight*scale),true);       // 将图片绑定到ImageVIEw       image.setimageBitmap(bitmap2);              /**        * setScaleType()可以设置当图片大小和容器大小不匹配时的剪辑模式.        * ScaleType.CENTER_INSIDE的意思是,按图片的原比例缩小或者扩大,直到长或者宽中的任何一个顶到容器为止        * 因为在上面我们已经重新生成了一个图片放在gallery上,大小已经符合要求了,所以这里就不需要再通过        * setScaleType进行剪裁了。有些人可能觉得笔者脑子有病,有简单的不用,非要用那么复杂的,这里呢,其实只是        * 给大家的一个使用范例,我们通过bitmap可不仅仅是缩放图片哦,还可以倾斜、移动等等……        */       // image.setScaleType(ScaleType.CENTER_INSIDE);       image.setAdjustVIEwBounds(true);// 自适应边框处理       image.setLayoutParams(new gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));       return image;     }    }   /**    * 当gallery的图像发生变化时,同步ImageSwitcher的变化    */   @OverrIDe   public voID onItemSelected(AdapterVIEw<?> parent,long ID) {     imageSwitcher.setimageResource(imagesID[position]);    }    @OverrIDe   public voID onnothingSelected(AdapterVIEw<?> parent) {    }   int i = 0 ;   /***    * 屏幕被触摸时提供的方法,这里我们通过它来实现拖动大图片,也会自动切换图片o    */   @OverrIDe   public boolean ontouch(VIEw v,MotionEvent event) {     Log.i("是否正在触摸","yes");     int actionname = event.getAction();     float []position = null;          //被按下时     if(actionname==MotionEvent.ACTION_DOWN){       i = 0;     }     // 被按着拖动时     if(actionname == MotionEvent.ACTION_MOVE)     {       position[i] = event.getX();       ++i;            }     if(actionname == MotionEvent.ACTION_UP)     {       if(position[position.length]>position[position.length-1]){                  if((index+1)>imagesID.length){           imageSwitcher.setimageResource(imagesID[index]);         }         else{           imageSwitcher.setimageResource(imagesID[index+1]);         }                         }       else{         if((index-1)<0){           imageSwitcher.setimageResource(imagesID[0]);         }         else{           imageSwitcher.setimageResource(imagesID[index-1]);         }                }     }     return true;   }  } 

gallery.xml

<?xml version="1.0" enCoding="utf-8"?> <linearLayout  xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:layout_wIDth="fill_parent"  androID:layout_height="fill_parent"  androID:orIEntation="vertical"  androID:gravity="center_horizontal"  >    <ImageSwitcher   androID:ID="@+ID/imageSwitcher"   androID:layout_height="350dp"   androID:layout_wIDth="fill_parent"   ></ImageSwitcher>      <gallery androID:ID="@+ID/gallery1"    androID:gravity="center"   androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"></gallery> </linearLayout> 

总结

以上是内存溢出为你收集整理的Android应用中图片浏览时实现自动切换功能的方法详解全部内容,希望文章能够帮你解决Android应用中图片浏览时实现自动切换功能的方法详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1149476.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存