2015年3月5日 星期四

[Android] GridView練習

看網路上的教學寫了一個小程式, 利用GridView列出所有在Drawable裡面的圖片(這裡放入12生肖圖), 點入其中一張圖跳轉到另一個Activity顯示完整的圖, 原本是想在這個Activity做Crop的動作, 但是還在找相關的教學文章, 所以還沒有implement


MainActivity.java
public class MainActivity extends ActionBarActivity {
    private GridView gridView;
    public static int[] image = {
            R.drawable.mouse,
            R.drawable.cow,
            R.drawable.tiger,
            R.drawable.rabbit,
            R.drawable.dragon,
            R.drawable.snake,
            R.drawable.horse,
            R.drawable.goat,
            R.drawable.monkey,
            R.drawable.chicken,
            R.drawable.dog,
            R.drawable.pig
    };

    private String[] imgText = {"鼠","牛","虎","兔","龍","蛇","馬","羊","猴","雞","狗","豬"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        List> items = new ArrayList>();
        for(int i =0;i < imgText.length;i++)
        {
            Map item = new HashMap();
            item.put("image", image[i]);
            item.put("text",imgText[i]);
            items.add(item);
        }

        SimpleAdapter adapter = new SimpleAdapter(this, items,R.layout.grid_item, new String[]{"image","text"}, new int[]{R.id.image,R.id.text});

        gridView = (GridView)findViewById(R.id.main_page_gridview);
        gridView.setNumColumns(3);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(),"Your choice is "+imgText[position],Toast.LENGTH_SHORT).show();
                view.setBackgroundColor(Color.BLUE);
                Intent it = new Intent(MainActivity.this, FullImage.class);
                it.putExtra("img",position);
                startActivity(it);
            }
        });

    }



FullImage.java
public class FullImage extends ActionBarActivity {
    ImageView image;
    int image_index;
    Drawable drawable;
    Resources res;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_image);

        image = (ImageView)findViewById(R.id.image);
        image_index = getIntent().getExtras().getInt("img");

        res = getBaseContext().getResources();
        drawable = res.getDrawable(MainActivity.image[image_index]);

        image.setImageDrawable(drawable);
    }

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity">
 <GridView android:id="@+id/main_page_gridview"
 android:numColumns="auto_fit"
 android:gravity="center"
 android:columnWidth="100dp"
 android:stretchMode="columnWidth"
 android:layout_width="match_parent"
 android:layout_height="match_parent" />
 </LinearLayout>

activity_full_image.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
 android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.foxlinkimage.alex.gridviewtest.FullImage"
 android:id="@+id/relayout">
 <ImageView android:id="@+id/image"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
 <LinearLayout android:layout_below="@+id/image"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
 <Button android:id="@+id/crop"
 android:text="Crop"
 android:textSize="15sp"
 android:layout_width="80dp"
 android:layout_height="40dp" />
 </LinearLayout>
 </RelativeLayout>


grid_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/image" android:layout_marginTop="10dp" android:layout_width="100dp" android:layout_height="100dp" /> <TextView android:id="@+id/text" android:textSize="12sp" android:gravity="center" android:paddingTop="10dp" android:paddingBottom="10dp" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>

沒有留言:

張貼留言