andriod 获得drawable下所有图片

Posted gisoracle

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了andriod 获得drawable下所有图片相关的知识,希望对你有一定的参考价值。

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下所有图片的主要内容,如果未能解决你的问题,请参考以下文章

android中如何获得drawable的颜色值?

Android,想获取这个对象里面的Drawable应该使用那个方法?

怎么拿到 drawable 的 宽高

Android问题之res/raw和assets的区别及文件大小限制

android:thumb="@drawable/seeker"啥意思

android项目,往drawable文件夹里面导入图片,main.xml里能找到图片,但是在java文件中R.drawable。找不到