Gallery+ImageView实现图片浏览

    技术2022-05-19  21

    昨天小菜看到了View的Gallery,哇,好酷的组件,可以实现图片浏览和预览也。

    于是乎就参考了例子的代码弄了一个效果,后面才发现高级的ImageSwitcher也能实现这个功能,菜鸟就是菜鸟啊,看来还得加油。

    虽然这样,还是把自己的代码贴出来嘛。

    结构,继承关系

             public class Gallery extends AbsSpinner implements GestureDetector.OnGestureListener

                   public class ImageSwitcher extends ViewSwitcher

    main.xml

    <?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    />    <ImageView    android:id="@+id/imageview"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:src="@drawable/a">    </ImageView>    <Gallery xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gallery"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center_vertical"        android:spacing="2dp"        android:fadingEdge="none"        ></Gallery></FrameLayout>

    attrs.xml

    <?xml version="1.0" encoding="utf-8"?><resources>

    <declare-styleable name="Mygallery">

    <attr name="android:galleryItemBackground" />

    </declare-styleable>

    </resources>

    Mygallery.java

    这里我把全部都写在一个类里面的,不推荐这样做哈

    package com.eash;

    import android.app.Activity;import android.content.Context;import android.content.res.TypedArray;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;import android.widget.Toast;

    public class Mygallery extends Activity {

     /** Called when the activity is first created. */

     private Gallery gallery; public ImageView imageview;

     @Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);    imageview = (ImageView) findViewById(R.id.imageview);  gallery = (Gallery) findViewById(R.id.gallery);  gallery.setAdapter(new ImageAdapter(this));// 设置图片适配器

      // 设置监听器

      gallery.setOnItemClickListener(new OnItemClickListener() {

       public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,     long arg3) {    //Toast.makeText(Mygallery.this, "" + arg2,Toast.LENGTH_LONG).show();      //imageview.setImageResource(arg2);    imageview.setImageResource(((ImageView)arg1).getId());//我自己把这个改成getId,哈哈可以了    //imageview.setBackgroundResource(((ImageView)arg1).getImageMatrix());//例子是这样写的,但是我搞不懂为啥不对     Toast.makeText(Mygallery.this, "点击了第"+arg2+"张图片",Toast.LENGTH_LONG).show();        }  });

     }

    }

    class ImageAdapter extends BaseAdapter { int mGalleryItemBackground; private Context context;

     // 图片源数组

     private Integer[] imageInteger = { R.drawable.a, R.drawable.as, R.drawable.asd, R.drawable.asdf, };

     public ImageAdapter(Context c) {  context = c; // 读取styleable资源  TypedArray a =c.obtainStyledAttributes(R.styleable.Mygallery);  mGalleryItemBackground = a.getResourceId(    R.styleable.Mygallery_android_galleryItemBackground, 0);  a.recycle(); }

     public int getCount() {  return imageInteger.length; }

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

     public long getItemId(int position) {  // TODO Auto-generated method stub  return position; }

     public View getView(int position, View convertView, ViewGroup parent) {  ImageView imageView = new ImageView(context);  imageView.setImageResource(imageInteger[position]);  imageView.setId(imageInteger[position]);   imageView.setScaleType(ImageView.ScaleType.FIT_XY);  imageView.setLayoutParams(new Gallery.LayoutParams(120, 80));  imageView.setBackgroundResource(mGalleryItemBackground);   return imageView; }}

    恩  这样就实现了简单的图片浏览功能了,但是有一个问题我还是不知道怎么弄,

    就是图片切换的动作如何添加,问了群里的大牛们都说用ImageSwitcher,这也是后话了。

     

     


    最新回复(0)