码迷,mamicode.com
首页 > 其他好文 > 详细

andriod 获得drawable下所有图片

时间:2016-03-10 12:40:19      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

package com.example.yanlei.my1;

import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
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.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private MainActivity ard=this;
    private LinearLayout mainView=null;
    private TextView tv=null;
    private GridView gv=null;
    private int id=0x01080000;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainView=new LinearLayout(this);
        mainView.setOrientation(LinearLayout.VERTICAL);
        tv=new TextView(this);
        tv.setText("这里显示图标名称和id及其尺寸");
        gv_init();
        mainView.addView(tv);
        mainView.addView(gv);
        setContentView(mainView);
    }

    /*gv初始化*/
    void gv_init(){
        gv=new GridView(this);
        GridView.LayoutParams lp=new GridView.LayoutParams(-1,-1);
        gv.setLayoutParams(lp);
        gv.setNumColumns(GridView.AUTO_FIT);
        gv.setVerticalSpacing(10);
        gv.setHorizontalSpacing(10);
        gv.setAdapter(new ImageAdapter(this));
        gv.setFocusableInTouchMode(true);
        gv.setOnItemClickListener(new OnItemClickListener(){
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                tv.setText("图标名称:"+ard.getResources().getResourceEntryName(0x01080000+arg2)+"\n");
                tv.append("图标Id:0x"+Integer.toHexString(0x10800000+arg2)+"\n");
                tv.append("图标大小:"+arg1.getWidth()+"x"+arg1.getHeight()+"\n");
            }
        });
        gv.setOnItemSelectedListener(new OnItemSelectedListener(){
            public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
                tv.setText("图标名称:"+ard.getResources().getResourceEntryName(0x01080000+arg2)+"\n");
                tv.append("图标Id:0x"+Integer.toHexString(0x10800000+arg2)+"\n");
                tv.append("图标大小:"+arg1.getWidth()+"x"+arg1.getHeight()+"\n");
            }
            public void onNothingSelected(AdapterView<?> arg0) {

            }
        });
    }

    class MyThread extends Thread{

    }

    /*ImageAdapter*/
    public class ImageAdapter extends BaseAdapter {

        private Context context;

        public ImageAdapter(Context c) {context = c;}

        public int getCount() {return 152;}
        public Object getItem(int position) {return position;}
        public long getItemId(int position) {return position;}

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView iv = new ImageView(context);
            iv.setLayoutParams(new GridView.LayoutParams(-2,-2));
            id=0x01080000+position;
            iv.setImageResource(id);
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            return iv;
        }
    }

    public String getApplicationName() {
        PackageManager packageManager = null;
        ApplicationInfo applicationInfo = null;
        try {
            packageManager = getApplicationContext().getPackageManager();
            applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            applicationInfo = null;
        }
        String applicationName =
                (String) packageManager.getApplicationLabel(applicationInfo);
        return applicationName;
    }
    public void ShowMessage(String str) {
        new AlertDialog.Builder(this)
                .setTitle(getApplicationName())
                .setMessage(str)
                .show();

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

 

andriod 获得drawable下所有图片

标签:

原文地址:http://www.cnblogs.com/gisoracle/p/5261117.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!