在GalleryView中设置位图图像

时间:2012-03-06 05:59:38

标签: android galleryview

我正在开发一个应用程序,我想设置一个从服务器下载并设置为GalleryView的图像继承人的图像代码  ga.setAdapter(new ImageAdapter(this, Card_Image));

这里我将图像从Bitmap转换为drawable

imageBitmap[i]=BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                Card_Image[i]=new BitmapDrawable(imageBitmap[i]);

和ImageAdapter是

public class ImageAdapter extends BaseAdapter {

    private Context ctx;
    int imageBackground;
    Drawable[] image_data;
    String[] merchantname,cardname,expireddate,points_;
    TextView MerchantName,CardName,ExpiryDate,Points;
    public ImageAdapter(Context c, Drawable []card_image) {
        ctx = c;
        image_data = card_image;
        merchantname = merchant_name;
        cardname = card_name;
        expireddate = expired_date;
        points_=points;
        TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
        imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
        ta.recycle();
    }

    public int getCount() {

        return image_data.length;
    }

    public Object getItem(int arg0) {

        return arg0;
    }

    public long getItemId(int arg0) {

        return arg0;
    }

    public View getView(int position, View arg1, ViewGroup arg2) {
        ImageView i ;//= new ImageView(this.ctx);
        if (arg1 == null) { 
            i = new ImageView(this.ctx); 
        } else { 
            i = (ImageView) arg1; 
        } 
        //          ImageView iv = new ImageView(ctx);
        //          Drawable drawable = new BitmapDrawable(getResources(), image_data[position]);
        //          MerchantName=(TextView)findViewById(R.id.header1);
        //          CardName=(TextView)findViewById(R.id.header2);
        //          ExpiryDate=(TextView)findViewById(R.id.footer1);
        //          Points=(TextView)findViewById(R.id.footer2);
        //          MerchantName.setText(merchantname[position]);
        //          CardName.setText(cardname[position]);
        //          ExpiryDate.setText(expireddate[position]);
        //          Points.setText(points_[position]);
        i.setImageDrawable(image_data[position]);
        //          i.setImageDrawable(new BitmapDrawable(getResources(), image_data[position]));
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        i.setLayoutParams(new Gallery.LayoutParams(300,200));
        i.setBackgroundResource(imageBackground);
        return i;
    }

}

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="60dp"
android:background="@drawable/bckcolor"
android:orientation="vertical" >

<TextView
    android:id="@+id/Heder_HomeCard"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:background="@drawable/header"
    android:gravity="center"
    android:text="Cards"
    android:textColor="#FFFFFF"
    android:textSize="18sp"
    android:textStyle="bold" />

<AutoCompleteTextView
    android:id="@+id/card_search"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    android:background="@drawable/my_border"
    android:hint="Search"
    android:singleLine="true" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/header1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:text="ABC"
        android:textColor="#000"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/header2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:text="bcd"
        android:textColor="#000"
        android:textStyle="bold" />
</LinearLayout>

<LinearLayout
    android:id="@+id/addscrollviw"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="3.4"
    android:background="#FFFFFF"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="6"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:weightSum="1" >

        <Gallery
            android:id="@+id/Gallery01"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </Gallery>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/footer1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="25dp"
            android:gravity="left"
            android:text="xyz"
            android:textColor="#000"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/footer2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="45dp"
            android:gravity="right"
            android:text="jkl"
            android:textColor="#000"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="@drawable/header"
        android:gravity="center_horizontal"
        android:weightSum="1" >

        <ImageView
            android:id="@+id/star_card"
            android:layout_width="100dp"
            android:layout_height="fill_parent"
            android:layout_marginRight="50dp"
            android:layout_marginTop="5dp" />
        <!-- <Button android:id="@+id/button1" android:layout_height="34dp" -->
        <!-- android:textColor="#FFFFFF" android:layout_width="wrap_content" -->
        <!-- android:layout_weight="0.21" android:layout_marginRight="30dp" -->
        <!-- android:layout_marginTop="8dp" -->
        <!-- android:background="#348781" android:text="All Cards"></Button> -->

        <ImageView
            android:id="@+id/btn_addcard"
            android:layout_width="100dp"
            android:layout_height="fill_parent"
            android:layout_marginTop="5dp" />
        <!-- <Button android:text="Add Cards" android:id="@+id/btn_addcard" -->
        <!-- android:background="#348781" android:layout_width="wrap_content" -->
        <!-- android:layout_marginTop="8dp" android:layout_height="34dp" -->
        <!-- android:layout_weight="0.12" android:textColor="#FFFFFF"></Button> -->

    </LinearLayout>
</LinearLayout>

显示的是GalleryView,但只有黑色方块我无法在galleryView中看到图像 帮助

感谢你 阿布舍克巴克

0 个答案:

没有答案