0

我正在尝试使用自定义 arrayadapter 填充画廊。我正在尝试使用 oudated 画廊方法,其中选择的图片在使用 Onclick 单击后会放大显示。现在它已填充,但所有图像都已完全放大并可滚动。onClick 中的 toast 消息可以正常工作,但图库不能正常工作。

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Gallery gallery = (Gallery) findViewById(R.id.gallery1);

    Context ctx = getApplicationContext();
    Resources res = ctx.getResources();

    String[] options = res.getStringArray(R.array.image_names);
    TypedArray icons = res.obtainTypedArray(R.array.images);

    gallery.setAdapter(new ArrayImageAdapter(ctx, R.layout.main,
            options, icons));

    TypedArray imageIDs = res.obtainTypedArray(R.array.images);

    gallery.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View v,
        int position, long id)
        {
            Toast.makeText(getBaseContext(),
                    "pic" + (position + 1) + " selected",
                    Toast.LENGTH_SHORT).show();

            //---display the images selected---
            ImageView imageView = (ImageView) findViewById(R.id.image1);
            imageView.setImageResource(position);
        }
    });
}



public class ArrayImageAdapter extends ArrayAdapter<String> {

    private LayoutInflater mInflater;

    private String[] mStrings;
    private TypedArray mIcons;

    private int mViewResourceId;

    public ArrayImageAdapter(Context ctx, int viewResourceId,
            String[] strings, TypedArray icons) {
        super(ctx, viewResourceId, strings);

        mInflater = (LayoutInflater)ctx.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);

        mStrings = strings;
        mIcons = icons;

        mViewResourceId = viewResourceId;
    }

    @Override
    public int getCount() {
        return mStrings.length;
    }

    @Override
    public String getItem(int position) {
        return mStrings[position];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = mInflater.inflate(mViewResourceId, null);

        ImageView iv = (ImageView)convertView.findViewById(R.id.image1);
        iv.setImageDrawable(mIcons.getDrawable(position));

        TextView tv = (TextView)convertView.findViewById(R.id.text1);
        tv.setText(mStrings[position]);

        return convertView;
    }
}

}

4

1 回答 1

0

您要在 ArrayAdapter 的 getView 方法中使用的 imageView 的方法是ImageView.setScaleTypeImageView.setLayoutParams。这将缩小您的图像。

imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));

在这里,它解释了 LayoutParams:http: //developer.android.com/reference/android/view/View.html#setLayoutParams(android.view.ViewGroup.LayoutParams)

于 2015-11-09T02:29:41.993 回答