首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android Gallery图片展示和文字提示及Menu 菜单

2012-06-28 
Android Gallery图片显示和文字提示及Menu 菜单?Gallery组件主要用于横向显示图像列表,不过按常规做法。Gal

Android Gallery图片显示和文字提示及Menu 菜单

?

Gallery组件主要用于横向显示图像列表,不过按常规做法。Gallery组件只能有限地显示指定的图像。也就是说,如果为Gallery组件指定了10张图像,那么当Gallery组件显示到第10张时,就不会再继续显示了。

代码里带注释,欢迎相互讨论

Android Gallery图片展示和文字提示及Menu 菜单

Android Gallery图片展示和文字提示及Menu 菜单

Android Gallery图片展示和文字提示及Menu 菜单

Android Gallery图片展示和文字提示及Menu 菜单

Layout里的main.xml文件:

?

?

?

?res/menu/menu.xml文件:

?

?<item android:id="@+id/menu_about" android:title="关于我们" android:icon="@drawable/icon_about"/>

?

?

id标签,title是当前菜单的描述,icon是当前菜单的图片

?

package com.king.showgallery;  import android.content.Context;  import android.content.res.Resources;  import android.graphics.Bitmap;  import android.graphics.Bitmap.Config;  import android.graphics.BitmapFactory;  import android.graphics.Canvas;  import android.graphics.Color;  import android.graphics.LinearGradient;  import android.graphics.Matrix;  import android.graphics.Paint;  import android.graphics.PorterDuff.Mode;  import android.graphics.PorterDuffXfermode;  import android.graphics.Shader.TileMode;  import android.graphics.Typeface;  import android.graphics.drawable.BitmapDrawable;  import android.graphics.drawable.Drawable;  import android.view.View;  import android.view.ViewGroup;  import android.widget.BaseAdapter;  import android.widget.ImageView;  import android.widget.ImageView.ScaleType;  public class ImageAdapter extends BaseAdapter{      private Context mContext;      private Integer[] mImageIds;      private ImageView[] mImages;      private String[] strings;      private int width,height;            //图片上的提示信息      private String[] textList={"功能说明","项目说明","玩法说明","软件说明","退出","程序更新","兑奖","查询"};      public ImageAdapter(Context c, Integer[] ImageIds) {          mContext = c;          mImageIds = ImageIds;          mImages = new ImageView[mImageIds.length];      }      public boolean createReflectedImages() {          final int reflectionGap = 4;          int index = 0;                    for (int imageId : mImageIds) {              Bitmap originalImage = BitmapFactory.decodeResource(mContext                      .getResources(), imageId);              width = originalImage.getWidth();              height = originalImage.getHeight();              Matrix matrix = new Matrix();  //          matrix.setRotate(30);              matrix.preScale(1, -1);              Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,                      height / 2, width, height / 2, matrix, false);              Bitmap bitmapWithReflection = Bitmap.createBitmap(width,                      (height + height / 2), Config.ARGB_8888);              Canvas canvas = new Canvas(bitmapWithReflection);              canvas.drawBitmap(originalImage, 0, 0, null);              Paint deafaultPaint = new Paint();                            deafaultPaint.setAntiAlias(true);              canvas.drawRect(0, height, width, height + reflectionGap,                      deafaultPaint);              canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);              Paint paint = new Paint();                            paint.setAntiAlias(true);              LinearGradient shader = new LinearGradient(0,                      originalImage.getHeight(), 0,                      bitmapWithReflection.getHeight() + reflectionGap,                      0x70ffffff, 0x00ffffff, TileMode.CLAMP);              paint.setShader(shader);              paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));              canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()                      + reflectionGap, paint);              ImageView imageView = new ImageView(mContext);              imageView.setImageBitmap(bitmapWithReflection);              imageView.setLayoutParams(new GameGallery.LayoutParams(180, 240));                            mImages[index++] = imageView;                                      }          return true;      }      private Resources getResources() {          return null;      }      public int getCount() {          return mImageIds.length;      }      public Object getItem(int position) {          return position;      }      public long getItemId(int position) {          return position;      }      public View getView(int position, View convertView, ViewGroup parent) {          //给图片添加文字提示信息          Bitmap newb = Bitmap.createBitmap( width,height, Config.ARGB_8888 );              Canvas canvasTemp = new Canvas( newb );              canvasTemp.drawColor(Color.alpha(0));              Paint p = new Paint();              String familyName = "宋体";              Typeface font = Typeface.create(familyName, Typeface.BOLD);              p.setColor(Color.WHITE);              p.setTypeface(font);              p.setTextSize(30);              canvasTemp.drawText(textList[position], 40,80, p);              Drawable drawable = new BitmapDrawable(newb);              mImages[position].setBackgroundDrawable(drawable);                    return mImages[position];      }      public float getScale(boolean focused, int offset) {          return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));      }  }  

?在ImageAdapter类中有两个非常重要的方法:getCount和getView。其中getCount方法用于返回图像总数,要注意的是,这个总数不能大于图像的实际数(可以小于图像的实际数),否则会抛出越界异常。当Gallery组件要显示某一个图像时,就会调用getView方法,并将当前的图像索引(position参数)传入该方法。一般getView方法用于返回每一个显示图像的组件(ImageView对象)。从这一点可以看出,Gallery组件是即时显示图像的,而不是一下将所有的图像都显示出来。在getView方法中除了创建了ImageView对象,还用从resIds数组中获得了相应的图像资源ID来设置在ImageView中显示的图像。最后还设置了Gallery组件的背景显示风格。




在ImageAdapter里给图片添加文字信息,注意

1.Bitmap,可以来自资源/文件,也可以在程序中创建,实际上的功能相当于图片的存储空间;?

2.Canvas,紧密与Bitmap联系,把Bitmap比喻内容的话,那么Canvas就是提供了众多方法操作Bitamp的平台;?

3.Paint,与Canvas紧密联系,是"画板"上的笔刷工具,也用于设置View控件上的样式;?

4.Drawable,如果说前三者是看不见地在内存中画图,那么Drawable就是把前三者绘图结果表现出来的接口。Drawable多个子类,例如:位图(BitmapDrawable)、图形(ShapeDrawable)、图层(LayerDrawable)等。?

?

热点排行
Bad Request.