Android图库显示所选图像 [英] Android Gallery display selected image

查看:126
本文介绍了Android图库显示所选图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面codeI正在尝试显示在gallery.But选择,我收到了默认的Andr​​oid形象,不是我saved.What图像的图像我做错了如何解决这个..

I the following code i am trying to display the image that is selected in the gallery.But i get a default android image and not the image that i saved.What am i doing wrong how to resolve this..

package HGallery.com;

import HGallery.com.R;
import HGallery.com.HGalleryActivity.ImageAdapter;
import android.app.Activity;
import android.os.Bundle;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

public class HGalleryActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final ImageView iv=(ImageView) findViewById(R.id.imageView1);
    iv.setVisibility(View.INVISIBLE);



    Gallery g = (Gallery) findViewById(R.id.gallery);
    g.setAdapter(new ImageAdapter(this));

    g.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            //Toast.makeText(HGalleryActivity.this, "here", Toast.LENGTH_SHORT).show();
            //Toast.makeText(HGalleryActivity.this, "" + position, Toast.LENGTH_SHORT).show();

            iv.setVisibility(View.VISIBLE);
            Drawable d=getResources().getDrawable(R.drawable.icon);
            //Toast.makeText(HGalleryActivity.this, ""+d, Toast.LENGTH_SHORT).show();
            iv.setImageDrawable(d);     


        }
    });



}
public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;

    private Integer[] mImageIds = {
            R.drawable.sample_1,
            R.drawable.sample_2,
            R.drawable.sample_3

    };

    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
        mGalleryItemBackground = a.getResourceId(
                R.styleable.HelloGallery_android_galleryItemBackground, 0);
        a.recycle();
    }

    public int getCount() {
        return mImageIds.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 i = new ImageView(mContext);

        i.setImageResource(mImageIds[position]);
        i.setLayoutParams(new Gallery.LayoutParams(150, 100));
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        i.setBackgroundResource(mGalleryItemBackground);

        return i;
    }

/*  public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        return null;
    }*/
}
}

main.xml中

main.xml

<?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"
>
<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello"
/>
  <Gallery xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView  android:id="@+id/imageView1" android:layout_width="320px" android:layout_height="250px"></ImageView>
<!-- <ImageView android:layout_width="wrap_content" android:id="@+id/imageView2" android:layout_height="wrap_content" android:src="@drawable/icon"></ImageView>-->

推荐答案

您ImageAdapter内的方法叫做公开对象的getItem(INT位置)应进行修改,以返回合适 mImageIds [] 使用位置参数。然后,在 onItemClick ,你可以叫 parent.getItemAtPosition(位置)来获取资源ID。

The method inside your ImageAdapter called public Object getItem(int position) should be modified to return the appropriate mImageIds[] by using the position argument. Then, inside onItemClick, you can call parent.getItemAtPosition(position) to get the resource id.

如果您要使用比IDS其他东西,方法,你可以写自己的get方法(我会打电话getResourceId),并调用((ImageAdapter)parent.getAdapter() ).getResourceId(位置))

If you want to use that method for things other than the ids, you can write your own get method (which I'd call getResourceId), and call ((ImageAdapter)parent.getAdapter()).getResourceId(position)).

这篇关于Android图库显示所选图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆