傻A与傻C之间 - Sabby(一个徘徊在傻A与傻C之间的人)

欢迎来到傻A与傻C之间的博客,开发人员请加 C#/Java编程交流群:26438718、96020642

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

这周利用休息时间又学习了Gallery,然后随便写了一个图片浏览的东东。

首先看一下界面:

点击下面的图片,在上面相应的显示出来。

代码如下:

View Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/star_red"
            android:scaleType="matrix" 
            android:layout_weight="4"
            />

        <Gallery
            android:id="@+id/gallery"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:spacing="5dp" >

        </Gallery>

</LinearLayout>
View Code
  1 package com.example.griview;
  2 
  3 import javax.xml.parsers.SAXParser;
  4 import javax.xml.parsers.SAXParserFactory;
  5 
  6 import org.xml.sax.helpers.DefaultHandler;
  7 import org.xmlpull.v1.XmlSerializer;
  8 
  9 import android.os.Bundle;
 10 import android.app.Activity;
 11 import android.app.ActivityManager;
 12 import android.content.Context;
 13 import android.view.Menu;
 14 import android.view.View;
 15 import android.view.ViewGroup;
 16 import android.view.ViewGroup.LayoutParams;
 17 import android.widget.AdapterView;
 18 import android.widget.AdapterView.OnItemClickListener;
 19 import android.widget.BaseAdapter;
 20 import android.widget.Gallery;
 21 import android.widget.GridView;
 22 import android.widget.ImageView;
 23 import android.widget.Toast;
 24 
 25 public class MainActivity extends Activity {
 26 
 27     private Gallery gallery;
 28     private ImageView imageView;
 29     protected void onCreate(Bundle savedInstanceState) {
 30         super.onCreate(savedInstanceState);
 31         setContentView(R.layout.relative);
 32         gallery=(Gallery) findViewById(R.id.gallery);
 33         imageView=(ImageView) findViewById(R.id.imageView);
 34         gallery.setAdapter(new ImageAdapter(this));
 35 
 36         gallery.setOnItemClickListener(new OnItemClickListener() {
 37 
 38             public void onItemClick(AdapterView<?> parent, View view, int position,
 39                     long id) {
 40                 imageView.setBackgroundResource(mThumbIds[position]);
 41                 //弹出单击的GridView元素的位置 
 42                 Toast.makeText(MainActivity.this, mThumbIds[position], Toast.LENGTH_LONG).show();
 43             }
 44         
 45         
 46         });
 47         
 48     }
 49 
 50     public class ImageAdapter extends BaseAdapter{
 51 
 52         private Context mContext;
 53         //声明ImageAdapter
 54         public ImageAdapter(Context context)
 55         {
 56             this.mContext=context;
 57         }
 58         //获取图片的个数
 59         public int getCount() {
 60             return mThumbIds.length;
 61         }
 62 
 63         //获取图片在库中的位置
 64         public Object getItem(int position) {
 65             return mThumbIds[position];
 66         }
 67 
 68         //获取图片在库中的位置
 69         public long getItemId(int position) {
 70             return 0;
 71         }
 72 
 73         public View getView(int position, View convertView, ViewGroup parent) {
 74             
 75             ImageView imageView;
 76             if(convertView==null)
 77             {
 78                 imageView=new ImageView(mContext);
 79                  imageView.setLayoutParams(new Gallery.LayoutParams(75, 75));  
 80                     imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);  
 81                     imageView.setPadding(8, 8, 8, 8);  
 82                 }
 83             else{  
 84                     imageView = (ImageView) convertView;  
 85                 } 
 86             
 87                 imageView.setImageResource(mThumbIds[position]);  
 88                 return imageView;  
 89         }
 90         
 91     }
 92     
 93     //展示图片
 94     private Integer[] mThumbIds={
 95             R.drawable.heart,
 96             R.drawable.heart_broken,
 97             R.drawable.heart_delete,
 98             R.drawable.heart_preferences,
 99             R.drawable.heart_edit,
100             R.drawable.heart_new,
101             R.drawable.star_red,
102             R.drawable.meishi3,
103             R.drawable.meishi4,
104             R.drawable.meishi5,
105     };
106     public boolean onCreateOptionsMenu(Menu menu) {
107         // Inflate the menu; this adds items to the action bar if it is present.
108         getMenuInflater().inflate(R.menu.main, menu);
109         return true;
110     }
111 
112 }

这周就学到这点,期待下一次学习的进步...

posted on 2013-04-02 21:15  sabby  阅读(714)  评论(0编辑  收藏  举报