简体   繁体   English

是否可以创建图像的自定义视图?

[英]Is it possible to create a custom View of images?

I want to create a simple layout of images where it can be viewed in columns and rows like a table. 我想创建图像的简单布局,在其中可以像表一样在列和行中查看它。 Does anyone know if this is possible 有谁知道这是否可能

Grid View Code: 网格查看代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" >
    </GridView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Button" />

</RelativeLayout>

ImageAdapter: ImageAdapter:

public class ImageAdapter extends BaseAdapter {
    private Context mContext;


 // Keep all Images in array
    public Integer[] mThumbIds = {

            //R.drawable.1
        R.drawable.a,R.drawable.b,
        R.drawable.c,R.drawable.d,
        R.drawable.g,R.drawable.f,
        R.drawable.h,R.drawable.i,
        R.drawable.j,R.drawable.k,
        R.drawable.l,R.drawable.m

    };

    // Constructor
    public ImageAdapter(Context c){
        mContext = c;
    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mThumbIds[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub

         ImageView imageView = new ImageView(mContext);
            imageView.setImageResource(mThumbIds[position]);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
            return imageView;


    }


}

MainActivity: 主要活动:

 @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
              GridView gridView = (GridView) findViewById(R.id.gridView1);
              Button btnNavigate=(Button)findViewById(R.id.button1);

              btnNavigate.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(getBaseContext(), SpinnerDemo.class);
                    startActivity(intent);
                }
            });
                // Instance of ImageAdapter Class
                gridView.setAdapter(new ImageAdapter(this));
        }

Many Demos are available on androidhive.info androidhive.info上有许多演示

Try out as below: 尝试如下:

Layout file : 布局文件:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <GridView android:id="@+id/gridView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:columnWidth="90dp" android:gravity="center" android:horizontalSpacing="10dp" android:numColumns="auto_fit" android:stretchMode="columnWidth" android:verticalSpacing="10dp" android:layout_below="@+id/Linearlayout"> </GridView> <LinearLayout android:id="@+id/Linearlayout" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout> <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_below="@+id/gridView1" android:text="TextView" /> </RelativeLayout> 

AdapterClass 适配器类

  public class ImageAdapter extends BaseAdapter { private Context mContext; // Keep all Images in array public Integer[] mThumbIds = { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher }; // Constructor public ImageAdapter(Context c) { mContext = c; } @Override public int getCount() { // TODO Auto-generated method stub return mThumbIds.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return mThumbIds[position]; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View arg1, ViewGroup arg2) { // TODO Auto-generated method stub ImageView imageView = new ImageView(mContext); imageView.setImageResource(mThumbIds[position]); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setLayoutParams(new GridView.LayoutParams(70, 70)); return imageView; } } 

Call the adapter class to bind the data into GridView as below in your activity 调用适配器类将数据绑定到GridView中,如下所示:

  public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GridView m_grid=(GridView)findViewById(R.id.gridView1); m_grid.setAdapter(new ImageAdapter(getApplicationContext())); } 

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM