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);
}
}

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com