如何创建透明缩略图库?

时间:2014-01-12 09:17:26

标签: android image gallery image-gallery

那里,我将在我的应用程序上创建Android Gallery,但这并不完美,因为使用旧界面,我希望我的画廊看起来像现代用户界面,你能帮忙,我的画廊看起来像这样:

enter image description here

我希望我的画廊看起来像这样:)

enter image description here

这是我的代码:

  <?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"
    android:background="@null"

>

 <Gallery
    android:id="@+id/gallery1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@null"

  />

<ImageView
   android:id="@+id/image1"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:scaleType="matrix" 
   android:background="@null"/>

 </LinearLayout>

和我的活动:

   public class MainActivity extends Activity {
//---raw---
Integer[] imageID = {
    R.drawable.gbr1,
    R.drawable.gbr2,
    R.drawable.gbr3,
    R.drawable.gbr4
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Gallery gallery = (Gallery) findViewById(R.id.gallery1);
    gallery.setAdapter(new ImageAdapter(this));
    gallery.setOnItemClickListener(new OnItemClickListener()
    {
      public void onItemClick(AdapterView<?> parent, View v, int position, long id)
      {
        Toast.makeText(getBaseContext(), "Foto" + (position + 1) + " dipilih", Toast.LENGTH_SHORT).show();

        //---show click img---
        ImageView imageView = (ImageView) findViewById(R.id.image1);
        imageView.setImageResource(imageID[position]);
      }
    });
}

public class ImageAdapter extends BaseAdapter {
    private Context context;
    private int itemBackground;

    public ImageAdapter(Context c) {
        context = c;
        //---style---
        TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
        itemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
        a.recycle();
    }

    //--- back  mount picture---
    public int getCount() {
        return imageID.length;
    }

    //---back ID item---
    public Object getItem(int position) {
        return position;
    }

    //---back ID item---
    public long getItemId(int position) {
        return position;
    }

    //---back view ImageView---
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(imageID[position]);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
        imageView.setBackgroundResource(itemBackground);
        return imageView;
    }
}       
}

感谢All之前:D

1 个答案:

答案 0 :(得分:0)

为此,您可以使用Github(github.com)中存在的传统组件。 或者自己设计(这是更灵活的方式!) 但它可能需要编写几个自定义控件来实现您想要的外观。 你附上的两张图片几乎都有相同的核心(Code Behind),它们之间的主要区别在于它们的设计。