如何在Android中的Gridview中实现图库?

如何在Android中的Gridview中实现图库?,第1张

概述我有一个要求,我想用GridView实现一个图库.我尝试在Android开发者网站上使用Hellogallery.但GridView不起作用.解决方法:将此XML用于布局:gallery.xml<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apkes/android"

我有一个要求,我想用GrIDVIEw实现一个图库.
我尝试在Android开发者网站上使用Hello gallery.但GrIDVIEw不起作用.

解决方法:

将此XML用于布局:gallery.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" androID:background="@drawable/bg_child">    <FrameLayout androID:ID="@+ID/FrameLayout01"        androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent">        <FrameLayout androID:ID="@+ID/linearLayout01"            androID:layout_gravity="top" androID:layout_height="50dp" androID:layout_wIDth="fill_parent">            <TextVIEw androID:ID="@+ID/TextVIEw01"                androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:textStyle="bold" androID:layout_gravity="center_vertical" androID:layout_marginleft="30dp" androID:gravity="center_vertical" androID:drawableleft="@drawable/photo_frame" androID:textcolor="@color/grey" androID:text="@string/photogallery_txt"></TextVIEw>            <button androID:layout_gravity="right" androID:ID="@+ID/btnMoreInfo" androID:layout_marginRight="5dp" androID:layout_margintop="5dp" androID:textStyle="bold" androID:background="@drawable/my_child_button" androID:layout_wIDth="100dp" androID:layout_height="40dp" androID:text="@string/moreinfo_txt"></button>        </FrameLayout>        <GrIDVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID"            androID:ID="@+ID/grIDvIEw" androID:layout_wIDth="fill_parent"            androID:layout_height="fill_parent" androID:columnWIDth="90dp"            androID:numColumns="auto_fit" androID:verticalSpacing="10dp"            androID:horizontalSpacing="10dp" androID:stretchMode="columnWIDth"            androID:gravity="center" androID:layout_gravity="bottom"            androID:layout_margintop="50dp"></GrIDVIEw>    </FrameLayout></linearLayout>

活动文件galleryPage.java

public class galleryPage extends Activity {    // private Integer[] mImageIDs = {R.drawable.splash, R.drawable.splash,    // R.drawable.splash, R.drawable.splash, R.drawable.splash,    // R.drawable.splash, R.drawable.splash};    private static Uri[] mUrls = null;    private static String[] strUrls = null;    private String[] mnames = null;    private GrIDVIEw grIDvIEw = null;    private Cursor cc = null;    private button btnMoreInfo = null;    private ProgressDialog myProgressDialog = null;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        // Todo auto-generated method stub        super.onCreate(savedInstanceState);        CommonFunctions.setLanguage(getBaseContext());        requestwindowFeature(Window.FEATURE_NO_Title);        setContentVIEw(R.layout.gallery);        btnMoreInfo = (button) findVIEwByID(R.ID.btnMoreInfo);        // It have to be matched with the directory in SDCard        cc = this.getContentResolver().query(                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null,                null);        // file[] files=f.Listfiles();        if (cc != null) {            myProgressDialog = new ProgressDialog(galleryPage.this);            myProgressDialog.requestwindowFeature(Window.FEATURE_NO_Title);            myProgressDialog.setMessage(getResources().getString(R.string.pls_wait_txt));            //myProgressDialog.setIcon(R.drawable.blind);            myProgressDialog.show();            new Thread() {                public voID run() {                    try {                        cc.movetoFirst();                        mUrls = new Uri[cc.getCount()];                        strUrls = new String[cc.getCount()];                        mnames = new String[cc.getCount()];                        for (int i = 0; i < cc.getCount(); i++) {                            cc.movetoposition(i);                            mUrls[i] = Uri.parse(cc.getString(1));                            strUrls[i] = cc.getString(1);                            mnames[i] = cc.getString(3);                            //Log.e("mnames[i]",mnames[i]+":"+cc.getColumnCount()+ " : " +cc.getString(3));                        }                    } catch (Exception e) {                    }                    myProgressDialog.dismiss();                }            }.start();         grIDvIEw = (GrIDVIEw) findVIEwByID(R.ID.grIDvIEw);         grIDvIEw.setAdapter(new ImageAdapter(this));         grIDvIEw.setonItemClickListener(new OnItemClickListener() {            public voID onItemClick(AdapterVIEw<?> parent, VIEw v,                  int position, long ID) {               Intent i = new Intent(galleryPage.this, BigImage.class);               Log.e("intent : ", ""+position);               i.putExtra("imgurls", strUrls);               i.putExtra("position", position);               startActivity(i);            }         });        }        btnMoreInfo.setonClickListener(new OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                // Todo auto-generated method stub                Intent i = new Intent(galleryPage.this, ChildLogin.class);                startActivity(i);            }        });    }    /**     * This class loads the image gallery in grID vIEw.     *     */    public class ImageAdapter extends BaseAdapter {        private Context mContext;        public ImageAdapter(Context c) {            mContext = c;        }        public int getCount() {            return cc.getCount();        }        public Object getItem(int position) {            return null;        }        public long getItemID(int position) {            return 0;        }        // create a new ImageVIEw for each item referenced by the Adapter        public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) {            VIEw v = convertVIEw;            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);            v = vi.inflate(R.layout.galchild, null);            try {                ImageVIEw imageVIEw = (ImageVIEw) v.findVIEwByID(R.ID.ImageVIEw01);                //imageVIEw.setScaleType(ImageVIEw.ScaleType.FIT_XY);                // imageVIEw.setpadding(8, 8, 8, 8);                Bitmap bmp = decodeURI(mUrls[position].getPath());                //BitmapFactory.decodefile(mUrls[position].getPath());                imageVIEw.setimageBitmap(bmp);                //bmp.                TextVIEw txtname = (TextVIEw) v.findVIEwByID(R.ID.TextVIEw01);                txtname.setText(mnames[position]);            } catch (Exception e) {            }            return v;        }    }    @OverrIDe    protected voID onStart() {        // Todo auto-generated method stub        super.onStart();        FlurryAgent.onStartSession(this, "***");    }    // @OverrIDe    // protected voID onStop() {    // Todo auto-generated method stub    // super.onStop();    // FlurryAgent.onEndSession(this);    // }    /**     * This method is to scale down the image      */    public Bitmap decodeURI(String filePath){        Options options = new Options();        options.inJustDecodeBounds = true;        BitmapFactory.decodefile(filePath, options);        // Only scale if we need to         // (16384 buffer for img processing)        Boolean scaleByHeight = Math.abs(options.outHeight - 100) >= Math.abs(options.outWIDth - 100);        if(options.outHeight * options.outWIDth * 2 >= 16384){            // Load, scaling to smallest power of 2 that'll get it <= desired dimensions            double sampleSize = scaleByHeight                ? options.outHeight / 100                : options.outWIDth / 100;            options.inSampleSize =                 (int)Math.pow(2d, Math.floor(                Math.log(sampleSize)/Math.log(2d)));        }        // Do the actual deCoding        options.inJustDecodeBounds = false;        options.inTempStorage = new byte[512];          Bitmap output = BitmapFactory.decodefile(filePath, options);        return output;    }}
总结

以上是内存溢出为你收集整理的如何在Android中的Gridview中实现图库?全部内容,希望文章能够帮你解决如何在Android中的Gridview中实现图库?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1103549.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存