如何缩放图像项GridView android?

时间:2015-04-17 14:56:13

标签: android android-layout

我有自定义gridview图像,但我不知道如何在点击它时在gridview中缩放项目。谁能帮助我? 当我点击它时,我尝试在项目gridview中缩放图像。 适配器中的此代码。 但它不起作用!

public View getView(final int position, View convertView, ViewGroup parent) {
    final Animation animScale = AnimationUtils.loadAnimation(mContext,
            R.anim.scale_anim);
    ObjMusicalType objMusical = mListMusical.get(position);
    if (convertView == null) {
        LayoutInflater inf = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = inf.inflate(R.layout.item_grid_type, null);
        imgMenu = (ImageView) convertView
                .findViewById(R.id.img_type_musical);
    }

    imgMenu.setImageResource(objMusical.getImageMusical());
    imgMenu.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            try {
                Toast.makeText(mContext, "Click OK", Toast.LENGTH_LONG)
                        .show();
                imgMenu.startAnimation(animScale);
            } catch (Exception e) {

                e.printStackTrace();
            }
        }
    });
    return convertView;

}

这是项目gridview的文件xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.starsoft.musical.custom.SquareImageView
        android:id="@+id/img_type_musical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

文件scale_anim。

<set xmlns:android="http://schemas.android.com/apk/res/android" >

<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXScale="1"
    android:fromYScale="1"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1.5"
    android:toYScale="1.5" >
</scale>

抱歉,这是我在stackover中提出问题的第一次! 感谢您的帮助

1 个答案:

答案 0 :(得分:0)

你应该将convertView.findViewById移动到if(convertView == null)块之外:

if (convertView == null) {
    LayoutInflater inf = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    convertView = inf.inflate(R.layout.item_grid_type, null);
}
imgMenu = (ImageView) convertView
    .findViewById(R.id.img_type_musical);

但是为了获得更好的性能,您应该使用像http://developer.android.com/guide/topics/ui/layout/listview.html

这样的convertView.setTag

而且在OnClickListener中,您需要更改为:

imgMenu.setImageResource(objMusical.getImageMusical());
imgMenu.setOnClickListener(new OnClickListener() {
    ImageView _imgMenu = imgMenu;
    @Override
    public void onClick(View v) {
        try {
            Toast.makeText(mContext, "Click OK", Toast.LENGTH_LONG)
                    .show();
            _imgMenu.startAnimation(animScale);
        } catch (Exception e) {

            e.printStackTrace();
        }
    }
});

避免所有OnClickListener在同一个ImageView上调用startAnimation。