總網頁瀏覽量

關於我自己

我的相片
人生的必修課是接受無常,人生的選修課是放下執著。

2012年1月15日 星期日

帶有圖片的Toast toast.setView(LinearLayout)









GridViewActivity.java

package com.tsots.GridView_ToastWithPhoto;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class PhotoActivity extends Activity 
{
 Context context = PhotoActivity.this;
 ImageAdapter adapter = new ImageAdapter(this);
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GridView gridview = (GridView) findViewById(R.id.gridview1);
        gridview.setNumColumns(3);
        gridview.setAdapter(adapter);

        gridview.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView parent, View v, int position, long id) 
            {
             toastWithPhoto(position, v);
            }
        });
    }
 
    //實作show出圖片的toast
    public void toastWithPhoto(int position, View v)
    {
     ImageView iv = new ImageView(context);
     photoChangeSize(position, iv);
     TextView tv = new TextView(context);
     tv.setText(String.valueOf(position+1));
     LinearLayout lay = new LinearLayout(context);
     //水平排列
     lay.setOrientation(LinearLayout.HORIZONTAL);
     Toast toast = Toast.makeText(context, tv.getText(), Toast.LENGTH_SHORT);
     View view = toast.getView();
     lay.addView(view);//toast加入文字  
     lay.addView(iv);  //toast加入圖片       
     toast.setView(lay);    
     toast.show();
    }
 
    //改變原圖的size
    public void photoChangeSize(int position, ImageView iv)
    {
     Matrix matrix = new Matrix();
     float scaleWidth = (float) 0.5;
     float scaleHeigh = (float) 0.5;
     matrix.postScale(scaleWidth, scaleHeigh);
     //將int轉成Bitmap
     Bitmap bmp = BitmapFactory.decodeResource(getResources(), adapter.mThumbIds[position]);
     int bmpWidth = bmp.getWidth();
     int bmpHeigh = bmp.getHeight();
     //改變Bitmap的size
     Bitmap resizebmp = Bitmap.createBitmap(bmp,0,0, bmpWidth, bmpHeigh, matrix, true);
     iv.setImageBitmap(resizebmp);
    }
}



我們預先將所需的圖檔放入/res/draeable/,命名pic_01.png~pic_34.png
由GridView.LayoutParams(110, 110)可以設定顯示圖片的寬高

ImageAdapter.java

package com.tsots.GridView_ToastWithPhoto;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter 
{
    private Context mContext;

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

    public int getCount() 
    {
        return mThumbIds.length;
    }

    public Object getItem(int position) 
    {
        return null;
    }

    public long getItemId(int position) 
    {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ImageView imageView;
        if (convertView == null) 
        {  
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(110, 110));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } 
        else 
        {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    public Integer[] mThumbIds = 
    {
      R.drawable.pic_01, R.drawable.pic_02,
            R.drawable.pic_03, R.drawable.pic_04,
            R.drawable.pic_05, R.drawable.pic_06,
            R.drawable.pic_07, R.drawable.pic_08,
            R.drawable.pic_09, R.drawable.pic_10,
            R.drawable.pic_11, R.drawable.pic_12,
            R.drawable.pic_13, R.drawable.pic_14,
            R.drawable.pic_15, R.drawable.pic_16,
            R.drawable.pic_17, R.drawable.pic_18,
            R.drawable.pic_19, R.drawable.pic_20,
            R.drawable.pic_21, R.drawable.pic_22,
            R.drawable.pic_23, R.drawable.pic_24,
            R.drawable.pic_25, R.drawable.pic_26,
            R.drawable.pic_27, R.drawable.pic_28,
            R.drawable.pic_29, R.drawable.pic_30,
            R.drawable.pic_31, R.drawable.pic_32,
            R.drawable.pic_33, R.drawable.pic_34
    };
}

沒有留言:

張貼留言