我正在使用 ImageSwitcher 来显示图片列表。当用户从 A.class 中的 Listview 中选择一个项目时,会调用 ImageSwitcher 并让用户查看图片。如果单击列表中的第 1 项,则应将一组可绘制对象传递给图像切换器。如果单击列表中的第 2 项,则应显示一组不同的图片。我一直在努力解决如何将可绘制对象从 A.class 传递到 ImageSwitcher。
总结:我想将一类从 A.class 中的可绘制对象传递给 ImageSwitcher,这样如果单击项目 1,在 ImageSwitcher 中:
private Integer[] mThumb = {R.Drawable.pic1, R.Drawable.pic2, R.Drawable.pic3}
如果单击第 2 项,我希望在 ImageSwitcher 中:private Integer[] mThumb = [R.Drawable.pic6, R.Drawable.pic7, R.Drawable.pic8}
从发送活动开始,到目前为止,我已经尝试了许多使用 putExtra putIntegralArrayExtra 等的排列方式。我相信这个问题的答案是非常基本的。我是java新手,还在(慢慢地)学习。谢谢你的帮助!
这是图像切换器的代码,因此您可以了解我所说的 int[] mThumb 的意思:
public class ImageSwitch1 extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory, OnTouchListener {
Matrix matrix = new Matrix();
Matrix eventMatrix = new Matrix();
final static int NONE = 0;
final static int DRAG = 1;
final static int ZOOM = 2;
int touchState = NONE;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.imageswitcher);
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView parent, View v, int position, long id) {
mSwitcher.setImageResource(mImage[position]);
final TextView tv = (TextView)findViewById(R.id.SwitcherText);
tv.setText(mText[position]);
}
public void onNothingSelected(AdapterView parent) {
}
public View makeView() {
ImageView i = new ImageView(this);
i.setScaleType(ScaleType.MATRIX);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
i.setOnTouchListener(this);
return i;
}
final static float MIN_DIST = 50;
static float eventDistance = 0;
static float centerX =0, centerY = 0;
public boolean onTouch(View v, MotionEvent event) {
ImageView i = (ImageView) v;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
//primary touch event starts: remember touch down location
touchState = DRAG;
centerX = event.getX(0);
centerY = event.getY(0);
eventMatrix.set(matrix);
break;
case MotionEvent.ACTION_POINTER_DOWN:
//secondary touch event starts: remember distance and center
eventDistance = calcDistance(event);
calcMidpoint(centerX, centerY, event);
if (eventDistance > MIN_DIST) {
eventMatrix.set(matrix);
touchState = ZOOM;
}
break;
case MotionEvent.ACTION_MOVE:
if (touchState == DRAG) {
//single finger drag, translate accordingly
matrix.set(eventMatrix);
matrix.setTranslate(event.getX(0) - centerX,
event.getY(0) - centerY);
} else if (touchState == ZOOM) {
//multi-finger zoom, scale accordingly around center
float dist = calcDistance(event);
if (dist > MIN_DIST) {
matrix.set(eventMatrix);
float scale = dist / eventDistance;
matrix.postScale(scale, scale, centerX, centerY);
}
}
// Perform the transformation
i.setImageMatrix(matrix);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
touchState = NONE;
break;
}
return true;
}
private float calcDistance(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
private void calcMidpoint(float centerX, float centerY,
MotionEvent event) {
centerX = (event.getX(0) + event.getX(1))/2;
centerY = (event.getY(0) + event.getY(1))/2;
}
private ImageSwitcher mSwitcher;
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumb.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView im = new ImageView(mContext);
im.setImageResource(mThumb[position]);
im.setAdjustViewBounds(true);
im.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
im.setBackgroundResource(R.drawable.picture_frame);
return im;
}
private Context mContext;
}
//these are the little pictures
private Integer[] mThumb = {
R.drawable.lp_image1_thumb, R.drawable.lp_image2_thumb, R.drawable.lp_image3_thumb,
R.drawable.lp_image4_thumb, R.drawable.lp_image5_thumb, R.drawable.lp_image6_thumb,
R.drawable.lp_image7_thumb, R.drawable.lp_image8_thumb, R.drawable.lp_image9_thumb,
R.drawable.lp_image10_thumb};