本文实例讲述了Android实现动态向Gallery中添加图片及倒影与3D效果的方法。分享给大家供大家参考,具体如下:
在Android中gallery可以提供一个很好的显示图片的方式,实现上面的效果以及动态添加数据库或者网络上下载下来的图片资源。我们首先实现一个自定义的Gallery类。
MyGallery.java:
package nate.android.Service;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
publicclass MyGallery extends Gallery {
private Camera mCamera =new Camera();
privateint mMaxRotationAngle =45;
privateint mMaxZoom =-120;
privateint mCoveflowCenter;
public MyGallery(Context context) {
super(context);
this.setStaticTransformationsEnabled(true);
}
public MyGallery(Context context, AttributeSet attrs) {
super(context, attrs);
this.setStaticTransformationsEnabled(true);
}
public MyGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.setStaticTransformationsEnabled(true);
}
publicint getMaxRotationAngle() {
return mMaxRotationAngle;
}
publicvoid setMaxRotationAngle(int maxRotationAngle) {
mMaxRotationAngle = maxRotationAngle;
}
publicint getMaxZoom() {
return mMaxZoom;
}
publicvoid setMaxZoom(int maxZoom) {
mMaxZoom = maxZoom;
}
privateint getCenterOfCoverflow() {
return (getWidth() - getPaddingLeft() - getPaddingRight()) /2
+ getPaddingLeft();
}
privatestaticint getCenterOfView(View view) {
return view.getLeft() + view.getWidth() /2;
}
protectedboolean getChildStaticTransformation(View child, Transformation t) {
finalint childCenter = getCenterOfView(child);
finalint childWidth = child.getWidth();
int rotationAngle =0;
t.clear();
t.setTransformationType(Transformation.TYPE_MATRIX);
if (childCenter == mCoveflowCenter) {
transformImageBitmap((ImageView) child, t, 0);
} else {
rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
if (Math.abs(rotationAngle) > mMaxRotationAngle) {
rotationAngle = (rotationAngle <0) ?-mMaxRotationAngle
: mMaxRotationAngle;
}
transformImageBitmap((ImageView) child, t, rotationAngle);
}
returntrue;
}
protectedvoid onSizeChanged(int w, int h, int oldw, int oldh) {
mCoveflowCenter = getCenterOfCoverflow();
super.onSizeChanged(w, h, oldw, oldh);
}
privatevoid transformImageBitmap(ImageView child, Transformation t,
int rotationAngle) {
mCamera.save();
final Matrix imageMatrix = t.getMatrix();
finalint imageHeight = child.getLayoutParams().height;
finalint imageWidth = child.getLayoutParams().width;
finalint rotation = Math.abs(rotationAngle);
// 在Z轴上正向移动camera的视角,实际效果为放大图片。
// 如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动。
mCamera.translate(0.0f, 0.0f, 100.0f);
// As the angle of the view gets less, zoom in
if (rotation < mMaxRotationAngle) {
float zoomAmount = (float) (mMaxZoom + (rotation *1.5));
mCamera.translate(0.0f, 0.0f, zoomAmount);
}
// 在Y轴上旋转,对应图片竖向向里翻转。
// 如果在X轴上旋转,则对应图片横向向里翻转。
mCamera.rotateY(rotationAngle);
mCamera.getMatrix(imageMatrix);
imageMatrix.preTranslate(-(imageWidth /2), -(imageHeight /2));
imageMatrix.postTranslate((imageWidth /2), (imageHeight /2));
mCamera.restore();
}
}
在布局文件中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
>
<LinearLayout
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:paddingTop="10px"
>
<LinearLayout
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/dishName" android:textSize="18pt"
android:text="菜名"
/>
<LinearLayout
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:paddingLeft="10px"
>
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/ds" android:textSize="18pt"
android:text="评分 : "
/>
<RatingBar
android:numStars="5" android:rating="3"
android:stepSize="0.2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:isIndicator="true"
android:id="@+id/dishScores" style="?android:attr/ratingBarStyleSmall"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="@+id/dishPrice" android:text="价格" android:textSize="18pt"
/>
</LinearLayout>
</LinearLayout>
<nate.android.Service.MyGallery
android:id="@+id/Gallery01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
<TextView
android:text="\n\n\n\n这里是关于每一道菜的信息,点击图片进入评论"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/gallery01"
android:paddingLeft="5px"
android:id="@+id/showHint"
/>
</LinearLayout>
在上面的XML文件中,我们使用了自定义的MyGallery。
然后顶一个ImageAdapter类继承自BaseAdapter。
package nate.android.Service;
import java.util.ArrayList;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.Shader.TileMode;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
publicclass ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private ArrayList<byte[]> dishImages =new ArrayList<byte[]>();
private ImageView[] mImages;
public ImageAdapter(Context c,ArrayList<byte[]> tmpDishImages) {
mContext = c;
dishImages = tmpDishImages;
mImages =new ImageView[dishImages.size()];
}
publicboolean createReflectedImages() {
finalint reflectionGap =4;
int index =0;
System.out.println("dishImages size "+ dishImages.size());
for (int i =0; i < dishImages.size(); ++i ) {
System.out.println("dishImage --- "+ dishImages.get(i));
Bitmap originalImage = BitmapFactory.decodeByteArray(dishImages.get(i), 0, dishImages.get(i).length);
int width = originalImage.getWidth();
int height = originalImage.getHeight();
Matrix matrix =new Matrix();
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();
canvas.drawRect(0, height, width, height + reflectionGap,
deafaultPaint);
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
Paint paint =new Paint();
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 GalleryFlow.LayoutParams(180, 240));
imageView.setLayoutParams(new MyGallery.LayoutParams(270, 360));
//imageView.setScaleType(ScaleType.MATRIX);
mImages[index++] = imageView;
}
returntrue;
}
private Resources getResources() {
returnnull;
}
publicint getCount() {
return dishImages.size();
}
public Object getItem(int position) {
return position;
}
publiclong getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
return mImages[position];
}
publicfloat getScale(boolean focused, int offset) {
return Math.max(0, 1.0f/ (float) Math.pow(2, Math.abs(offset)));
}
}
在这个类中构造函数需要传入将要在gallery中绘制的图片数据,(以byte[]类型的为例,因为我在存入sqlite以及从从网络下载下来的图片demo中都将其转成byte[]),同样我们使用
代码如下:Bitmap originalImage = BitmapFactory.decodeByteArray(dishImages.get(i), 0, dishImages.get(i).length);
在这篇文章有较详细的说明://www.jb51.net/article/88588.htm
将byte[]类型的图片数据“还原”。byte[]类型的图片源数据保存在一个ArrayList<byte[]>当中。这样我们为动态的实现在gallery中添加图片提供数据来源。
在下面的activity中使用我们自定义的baseAdapter以及Gallery。实现上图显示的效果。
使用实例类
package com.nate.wte2;
import java.io.IOException;
import java.util.ArrayList;
import nate.InfoService.DishInfo;
import nate.InfoService.StoreInfoService;
import nate.InfoService.WhichChoice;
import nate.NetConnection.GetConnectionSock;
import nate.android.Service.GalleryFlow;
import nate.android.Service.ImageAdapter;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import com.nate.wte.LocalSql.StoresInfoDB;
publicclass DishMenuActivity extends Activity {
private ArrayList<DishInfo> dishInfoList =new ArrayList<DishInfo>();
private TextView dishName;
private RatingBar dishScores;
private TextView dishPrice;
//3:send the dish's whole info to fill the activity(send the comments of the dish)
privateint flag3 =3;
WhichChoice choice3 =new WhichChoice(flag3);
private StoreInfoService storeInfo;
private ProgressDialog loadingDialog;
/**
* handler handle the dialog dismission
*/
private Handler handler =new Handler(){
@Override
publicvoid handleMessage(Message msg) {
loadingDialog.dismiss();
//other operation
super.handleMessage(msg);
}
};
/**
* thread to load the data from local database or from the server
* @author Administrator
*
*/
class Loading implements Runnable{
@Override
publicvoid run() {
try {
//这儿的sleep将换成一个循环,知道某个条件满足时候才结束循环,让dialog终止
Thread.sleep(1500);
handler.sendEmptyMessage(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* loading the items,start the thread to load
*/
publicvoid loadingItems(){
loadingDialog = ProgressDialog.show(DishMenuActivity.this, "", "loading...");
Thread t =new Thread(new Loading());
t.start();
}
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dishmenu_gallery);
StoresInfoDB infoDB;
int dishInfoListLength;
ArrayList<byte[]> dishImages =new ArrayList<byte[]>();
byte[] dishImage;
dishName = (TextView)this.findViewById(R.id.dishName);
dishPrice = (TextView)this.findViewById(R.id.dishPrice);
dishScores = (RatingBar)this.findViewById(R.id.dishScores);
//得到intent中从Choices类中传过来的对象
Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("bundleData");
storeInfo = (StoreInfoService)bundle.getSerializable("storeInfo");
dishInfoList = (ArrayList<DishInfo>)bundle.getSerializable("dishInfoList");
System.out.println("look look the info received from Choices Activity");
for(int i =0; i < dishInfoList.size(); i++){
System.out.println("--- "+ i + dishInfoList.get(i).getDishImage().toString());
}
dishInfoListLength = dishInfoList.size();
//初始化 dishImages
for(int i =0; i < dishInfoListLength; i++){
dishImages.add(dishInfoList.get(i).getDishImage());
}
System.out.println("the length of the dishImages ---- "+ dishImages.size());
////////////////////////////////
//注意这里一段
////////////////////////////////
ImageAdapter adapter =new ImageAdapter(DishMenuActivity.this,dishImages);
adapter.createReflectedImages();
GalleryFlow galleryFlow = (GalleryFlow) findViewById(R.id.Gallery01);
galleryFlow.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
publicvoid onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String showName ="菜名 : "+ dishInfoList.get((int)arg3).getDishName() +"";
dishName.setText(showName);
dishScores.setRating(dishInfoList.get((int)arg3).getDishScores());
dishPrice.setText("价格 : "+ dishInfoList.get((int)arg3).getPrice() +" 元\n\n\n");
}
@Override
publicvoid onNothingSelected(AdapterView<?> arg0) {
}
});
galleryFlow.setOnItemClickListener(new OnItemClickListener(){
@Override
publicvoid onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
loadingItems();
DishInfo dishInfo = dishInfoList.get((int)arg3);
try {
GetConnectionSock.fromClient.writeObject(choice3);
System.out.println("send the flag 3 ");
GetConnectionSock.fromClient.writeObject(dishInfo);
System.out.println("send the name back to server");
DishInfo dishComments = (DishInfo)GetConnectionSock.fromServer.readObject();
System.out.println("recv the dish comments");
dishInfo.setDishName(dishInfoList.get((int)arg3).getDishName());
dishInfo.setDishComments(dishComments.getDishComments());
System.out.println("full the dish info");
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Intent intent =new Intent();
Bundle bundle =new Bundle();
bundle.putSerializable("dishInfo",dishInfo);
bundle.putSerializable("storeInfo",storeInfo);
intent.putExtra("dishBundleData",bundle);
intent.setClass(DishMenuActivity.this,DishInfoDynamic.class);
Toast.makeText(DishMenuActivity.this, "进入评论此道菜",Toast.LENGTH_LONG).show();
DishMenuActivity.this.startActivity(intent);
}
});
galleryFlow.setAdapter(adapter); //注意这里
}
}
在这个activity中跟本文相关的,也就是在galley中添加图片功能,只需注意上面代码中标注出来的部分代码即可,至于数据来源得到的方式都不一样,这里只要知道数据是一个ArrayList<byte[]>就行了。重要的是利用上面的MyGallery以及ImageAdapter,当然,通过简单的理解,很轻松的这两个类就能够在其他的工程中重用的
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
您可能感兴趣的文章:Android多点触控实现对图片放大缩小平移,惯性滑动等功能基于Android 实现图片平移、缩放、旋转同时进行Android实现手势滑动多点触摸缩放平移图片效果Android实现手势滑动多点触摸缩放平移图片效果(二)Android单点触控实现图片平移、缩放、旋转功能Android实现对图片放大、平移和旋转的功能Android中利用matrix 控制图片的旋转、缩放、移动Android 图片缩放与旋转的实现详解android 图片操作(缩放移动) 实例代码Android编程实现图片的浏览、缩放、拖动和自动居中效果Android开发实现图片平移、缩放、倒影及旋转功能的方法