Android UI学习 - GridView和ImageView的使用
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.grid_2);
- GridView g = (GridView) findViewById(R.id.myGrid);
- g.setAdapter(new ImageAdapter(this));
- }
- public class ImageAdapter extends BaseAdapter {
- public ImageAdapter(Context c) {
- mContext = c;
- }
- public int getCount() {
- return mThumbIds.length;
- }
- public Object getItem(int position) {
- return position;
- }
- public long getItemId(int position) {
- return position;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- ImageView imageView;
- if (convertView == null) {
- imageView = new ImageView(mContext);
- imageView.setLayoutParams(new GridView.LayoutParams(45, 45));//设置ImageView宽高
- imageView.setAdjustViewBounds(false);
- imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
- imageView.setPadding(8, 8, 8, 8);
- } else {
- imageView = (ImageView) convertView;
- }
- imageView.setImageResource(mThumbIds[position]);
- return imageView;
- }
- private Context mContext;
- private Integer[] mThumbIds = {
- R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
- R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
- R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
- R.drawable.sample_thumb_6, R.drawable.sample_thumb_7
- };
- }
- <?xml version="1.0" encoding="utf-8"?>
- <GridView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/myGrid"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:padding="10dp"
- android:verticalSpacing="10dp"
- android:horizontalSpacing="10dp"
- android:numColumns="auto_fit"
- android:columnWidth="60dp"
- android:stretchMode="columnWidth"
- android:gravity="center"
- />
- //获得Bitmap的高和宽
- int bmpWidth=bmp.getWidth();
- int bmpHeight=bmp.getHeight();
- //设置缩小比例
- double scale=0.8;
- //计算出这次要缩小的比例
- scaleWidth=(float)(scaleWidth*scale);
- scaleHeight=(float)(scaleHeight*scale);
- //产生resize后的Bitmap对象
- Matrix matrix=new Matrix();
- matrix.postScale(scaleWidth, scaleHeight);
- Bitmap resizeBmp=Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
- <ImageView
- android:id="@+id/image"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:scaleType="center"
- android:src="@drawable/candle"
- />
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。