編輯:Android開發實例
Android的GridView控件用於把一系列的空間組織成一個二維的網格顯示出來應用的比較多的就是組合圖片顯示下面我就詳細講一個例子
首先寫一個類繼承BaseAdapter
- public class ImageList extends BaseAdapter {
- public Bitmap[] image;
- Activity activity;
- // construct
- public ImageList(Activity a) {
- activity = a;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return image.length;
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return image[position];
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- ImageView imageView;
- if (convertView == null) {
- imageView = new ImageView(activity);
- imageView.setLayoutParams(new GridView.LayoutParams(50, 50));// 設置ImageView寬高
- imageView.setAdjustViewBounds(false);
- imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
- } else {
- imageView = (ImageView) convertView;
- }
- imageView.setImageBitmap(image[position]);
- return imageView;
- }
- public static Bitmap drawableToBitmap(Drawable drawable) {
- Bitmap bitmap = Bitmap
- .createBitmap(
- drawable.getIntrinsicWidth(),
- drawable.getIntrinsicHeight(),
- drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
- : Bitmap.Config.RGB_565);
- Canvas canvas = new Canvas(bitmap);
- drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
- drawable.draw(canvas);
- return bitmap;
- }
- }
然後在主程序裡
- final GridView gv = (GridView) findViewById(R.id.grid);
- ImageList adapter = new ImageList(available.this);
- Resources res=getResources();
- Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.logo);
- adapter.image=new Bitmap[20];
- for(int i=0;i<=19;i++)
- {
- adapter.image[i]=bmp;
- }
- gv.setAdapter(adapter);
- gv.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> arg0, View arg1,
- int arg2, long arg3) {
- // TODO Auto-generated method stub
- Log.i("phb", arg2 + "");
- Log.i("phbsel",gv.isFocused()+"");
- Intent i=new Intent(available.this, com.mobile.links.shop.mycx.shopcart.class);
- startActivity(i);
- }
- });
- gv.setOnItemSelectedListener(new OnItemSelectedListener(){
- @Override
- public void onItemSelected(AdapterView<?> arg0, View arg1,
- int arg2, long arg3) {
- // TODO Auto-generated method stub
- Log.i("selected",arg2+"");
- tv.setText(arg2+":detail");
- }
- @Override
- public void onNothingSelected(AdapterView<?> arg0) {
- // TODO Auto-generated method stub
- }
- });
其中onClick函數是判斷被點擊
而onSelect函數是判斷被選中 也就是獲得焦點的imageview
這樣我們就可以基於gridview來做一些應用了。
構建gridview的xml代碼
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- android:id="@+id/relativelayout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- >
- <GridView android:id="@+id/availgrid"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:numColumns="4"
- android:verticalSpacing="30dp"
- android:layout_centerHorizontal = "true"
- />
- </LinearLayout>
Android提供了許多方法來控制播放的音頻/視頻文件和流。其中該方法是通過一類稱為MediaPlayer。Android是提供MediaPlayer類訪問內置的媒體播放
登錄應用程序的屏幕,詢問憑據登錄到一些特定的應用。可能需要登錄到Facebook,微博等本章介紹了,如何創建一個登錄界面,以及如何管理安全問題和錯誤嘗試。首先,必須定義兩
本文使用Matrix實現Android實現圖片縮放與旋轉。示例代碼如下:代碼如下:package com.android.matrix;import androi
可以顯示在的Android任務,通過加載進度條的進展。進度條有兩種形狀。加載欄和加載微調(spinner)。在本章中,我們將討論微調(spinner)。Spinner 用