如何在Android中实现带缩放功能的GridView

时间:2012-10-25 12:28:14

标签: android gridview zoom scale

我尝试实现GridView,但我对带有缩放功能的GridView有一些特殊要求。

在我的GridView中,我显示图像和文本。

我使用CustomAdapter填充GridView。

这里我显示了我的GridView图像。

enter image description here

在Android中可以吗?

1 个答案:

答案 0 :(得分:1)

图库类

   public class Gallery extends Activity implements OnClickListener{
     /** Called when the activity is first created. */
GridView grid_main;
Intent i;
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gridgallery);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));
   }
public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

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

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

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
            /**/
            imageView.setOnClickListener(new View.OnClickListener()
            {   
              public void onClick(View v) 
              {
                  for(int j=0;j<=mThumbIds.length;j++)
                  if(position==j)
                  {   
                      System.out.println(" image click");
                      i = new Intent("com.university.uob.ZoomImage");
                      i.putExtra("imageid", mThumbIds[j]);
                      startActivity(i);
                  }

                    }});


        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(mThumbIds[position]);


        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.u1, R.drawable.u2,
            R.drawable.u3, R.drawable.u4,
            R.drawable.u5, R.drawable.u6,
            R.drawable.u7, R.drawable.u8,
            R.drawable.u9
    };
}
   @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        super.onKeyDown(keyCode, event);
        switch(keyCode)
        {
        case KeyEvent.KEYCODE_BACK:

            finish();
            break;  
        }
        return super.onKeyDown(keyCode, event);
    }
public void onClick(View a) {
    //System.out.println("========a id====="+a.getId());
    // TODO Auto-generated method stub      
}
}

ZoomImage Class

 public class ZoomImage extends Activity{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.imagezoom);
    Bundle b = getIntent().getExtras();
    int imgid =  (Integer) b.get("imageid");
    System.out.println("======imagid======="+imgid);

    ImageView iv = (ImageView) findViewById(R.id.imageView1);
    iv.setImageResource(imgid);
}

   @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        super.onKeyDown(keyCode, event);
        switch(keyCode)
        {
        case KeyEvent.KEYCODE_BACK:
             finish();
             break; 
        }
        return super.onKeyDown(keyCode, event);
    }
   }

ImageZoom布局

      <LinearLayout 
    android:orientation="vertical"
    android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView
     android:id="@+id/imageView1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     xmlns:android="http://schemas.android.com/apk/res/android"></ImageView>
   </LinearLayout>

显示图像。 OnClick在图像视图中的新活动中显示它。我不确定这是不是你要找的。希望它有所帮助。