2010年1月17日星期日

android.R.drawable

Android提供一些自帶的drawable資源, 由17301504至17301655; 可以通過android.R.drawable存取.

這篇文章修改上一個練習"畫廊(Gallery), 及有一點動畫效果的圖像切換器(ImageSwitcher)", 演示Android的drawable資源.

android.R.drawable

修改main.xml添加一個TextView, 顯示drawable資源的ID.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:spacing="10dp"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/resourceid"
/>
<ImageSwitcher
android:id="@+id/imageswitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>


修改主程序, 主要在ImageAdapter的構造方法, 和OnItemSelectedListener的onItemSelected方法:
package com.AndroidGallery;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ViewSwitcher.ViewFactory;


public class AndroidGallery extends Activity implements ViewFactory{

final int resourceStart = 17301504;
final int resourceEnd = 17301655;
final int ResourceLength = resourceEnd - resourceStart + 1;

private ImageSwitcher myImageSwitcher;
private TextView resourceID;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

resourceID = (TextView)findViewById(R.id.resourceid);

myImageSwitcher = (ImageSwitcher)findViewById(R.id.imageswitcher);
myImageSwitcher.setFactory(this);
myImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
myImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));

Gallery myGallery = (Gallery)findViewById(R.id.gallery);
myGallery.setAdapter(new ImageAdapter(this));
myGallery.setOnItemSelectedListener(myGalleryOnItemSelectedListener);
}

private OnItemSelectedListener myGalleryOnItemSelectedListener
= new OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
myImageSwitcher.setImageResource(mThumbIds[arg2]);

String strResource =
arg0.getAdapter().getItem(arg2).toString() +
" " +
Resources.getSystem().getResourceName(arg2+resourceStart);
resourceID.setText(strResource);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

};

private int[] mThumbIds = new int[ResourceLength];

public class ImageAdapter extends BaseAdapter{
private Context context;

public ImageAdapter(Context c){
context = c;
for ( int i = 0; i < ResourceLength; i++)
{
mThumbIds[i] = i + resourceStart;
}
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mThumbIds[position];
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(context);
imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
}

@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView i = new ImageView(this);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return i;
}
}




1 則留言:

  1. 可以請問 resourceStart 是怎麼得知的
    ^^"困擾許久的圖片問題

    回覆刪除