Android控件Gallery用处非常多,下面讲解一种选择中到的例子:
效果图如下:
下面贴一下主要关键代码:
package com.example.gaga; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; 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; public class BitmapUtil { public static Bitmap createTxtImage(String txt, int txtSize) { Bitmap mbmpTest = Bitmap.createBitmap(txt.length() * txtSize + 4, txtSize + 4, Config.ARGB_8888); Canvas canvasTemp = new Canvas(mbmpTest); Paint p = new Paint(); p.setAntiAlias(true); p.setColor(Color.RED); p.setTextSize(txtSize); canvasTemp.drawText(txt, 2, txtSize - 2, p); return mbmpTest; } public static Bitmap createReflectedImage(Bitmap originalImage) { // The gap we want between the reflection and the original image final int reflectionGap = 0; int width = originalImage.getWidth(); int height = originalImage.getHeight(); // This will not scale but will flip on the Y axis Matrix matrix = new Matrix(); matrix.preScale(1, -1); // Create a Bitmap with the flip matrix applied to it. // We only want the bottom half of the image Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width, height / 2, matrix, false); // Create a new bitmap with same width but taller to fit reflection Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888); // Create a new Canvas with the bitmap that's big enough for // the image plus gap plus reflection Canvas canvas = new Canvas(bitmapWithReflection); // Draw in the original image canvas.drawBitmap(originalImage, 0, 0, null); // Draw in the gap Paint defaultPaint = new Paint(); canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint); // Draw in the reflection canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); // Create a shader that is a linear gradient that covers the reflection Paint paint = new Paint(); LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP); // Set the paint to use this shader (linear gradient) paint.setShader(shader); // Set the Transfer mode to be porter duff and destination in paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // Draw a rectangle using the paint with our linear gradient canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint); return bitmapWithReflection; } }
2.
package com.example.gaga; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.Gallery; public class CustomGallery extends Gallery{ public CustomGallery(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // TODO Auto-generated method stub return false; } }
3.
package com.example.gaga; public class University { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
4.
package com.example.gaga; import java.util.ArrayList; import android.content.Context; import android.graphics.Point; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.Toast; public class UniversityAdapter extends BaseAdapter { private Context context; private ArrayList<University> list; public UniversityAdapter(Context context, ArrayList<University> list) { super(); this.context = context; this.list = list; } @Override public int getCount() { // TODO Auto-generated method stub return Integer.MAX_VALUE; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } private int select=0; public void notifyDataSetChanged(int all){ select=all; super.notifyDataSetChanged(); } @Override public View getView(int position, View arg1, ViewGroup arg2) { // TODO Auto-generated method stub ImageView in=new ImageView(context); if (select==position) { University pro=list.get(position%list.size()); in.setImageBitmap(BitmapUtil.createReflectedImage(BitmapUtil.createTxtImage(pro.getName(), 28))); }else { University pro=list.get(position%list.size()); in.setImageBitmap(BitmapUtil.createTxtImage(pro.getName(), 22)); } return in; } }
5.
package com.example.gaga; import java.util.ArrayList; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.AdapterView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemSelectedListener; public class MainActivity extends Activity implements OnItemSelectedListener, OnItemClickListener { private CustomGallery glllery; private UniversityAdapter padapter; private ArrayList<University> list=new ArrayList<University>(); private void init(){ padapter=new UniversityAdapter(MainActivity.this, list); University p=new University(); p.setName("中国大学"); list.add(p); University p1=new University(); p1.setName("河南大学"); list.add(p1); University p2=new University(); p2.setName("郑州大学"); list.add(p2); University p3=new University(); p3.setName("北京交通大学"); list.add(p3); University p4=new University(); p4.setName("清华大学"); list.add(p4); University p5=new University(); p5.setName("北京大学"); list.add(p5); glllery.setAdapter(padapter); glllery.setSelection(Integer.MAX_VALUE/2); glllery.setOnItemSelectedListener(this); glllery.setOnItemClickListener(this); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); glllery=(CustomGallery) this.findViewById(R.id.gagagagaga); init(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub padapter.notifyDataSetChanged(arg2); // Toast.makeText(MainActivity.this, ""+arg2, Toast.LENGTH_LONG).show(); //Toast.makeText(MainActivity.this, arg2, 1).show(); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub position%list.size() Toast.makeText(MainActivity.this, ""+list.get(arg2%list.size()).getName(), Toast.LENGTH_LONG).show(); } }
6.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <com.example.gaga.CustomGallery android:id="@+id/gagagagaga" android:layout_width="fill_parent" android:layout_height="wrap_content" android:spacing="10dp" android:unselectedAlpha="0.5" android:layout_marginTop="40dp" /> </RelativeLayout>
本应用主要是复写了Gallery的一些方法,此控件的用处非常多,本次介绍的只是一种效果。后续继续对此控件进行总结!!!