一、引言
最近复习Android知识,使用到一个自定义相册,即Galley类的使用,遇到一些问题,现在记录和分享出来。
二、问题描述
自己写的Gallery类中,元素不在屏幕中间。效果如下:
三、自定义的Gallery代码:
package picturegame.view;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Gallery;
import android.widget.ImageView;
public class GalleryView extends Gallery{
private Camera mCamera = new Camera();
private int mMaxRotationAngle=45; //最大旋转角度
private int mMaxZoom=-120;
private int mCoveflowCenter;
public GalleryView(Context context){
this(context,null);
}
public GalleryView(Context context,AttributeSet attrs) {
// TODO Auto-generated constructor stub
this(context,attrs,0);
// super(context, attrs);
this.setStaticTransformationsEnabled(true);
}
public GalleryView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
this.setStaticTransformationsEnabled(true);
}
public int getmMaxRotationAngle() {
return mMaxRotationAngle;
}
public void setmMaxRotationAngle(int mMaxRotationAngle) {
this.mMaxRotationAngle = mMaxRotationAngle;
}
public int getmMaxZoom() {
return mMaxZoom;
}
public void setmMaxZoom(int mMaxZoom) {
this.mMaxZoom = mMaxZoom;
}
/*获取gallery中心x*/
private int getCenterOfCoverflow(){
int centerX=(getWidth() -getPaddingLeft()-getPaddingRight())/2 +getPaddingLeft();
System.out.println("Gallary x:"+centerX);
return centerX;
}
/*获取view中心x*/
private int getCenterOfView(View view ){
int centerX=view.getLeft() + view.getWidth()/2;
System.out.println("View x:"+centerX);
return centerX;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// TODO Auto-generated method stub
mCoveflowCenter=getCenterOfCoverflow();
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected boolean getChildStaticTransformation(View child, Transformation trans) {
// TODO Auto-generated method stub
final int childCenter = getCenterOfView(child);
final int childWidth = child.getWidth();
int rotationAngle = 0;
trans.clear();
trans.setTransformationType(Transformation.TYPE_BOTH); //透明度和比例都要变换
if(childCenter == mCoveflowCenter){
//中间的view
transformImageBitmap((ImageView)child, trans, 0);
}else{
//两侧的view
rotationAngle = (int) (((float)(mCoveflowCenter-childCenter)/childWidth) * mMaxRotationAngle);
if(Math.abs(rotationAngle)>mMaxRotationAngle){
rotationAngle =(rotationAngle <0)? -mMaxRotationAngle : mMaxRotationAngle;
}
transformImageBitmap((ImageView)child, trans, rotationAngle);
}
return true;
}
private void transformImageBitmap(ImageView child ,Transformation trans, int rotationAngle){
mCamera.save();
final Matrix imageMatrix = trans.getMatrix();
final int imageHeight = child.getLayoutParams().height;
final int imageWidth = child.getLayoutParams().width;
final int rotation =Math.abs(rotationAngle);
// 在Z轴上正向移动camera的视角,实际效果为放大图片; 如果在Y轴上移动,则图片上下移动; X轴上对应图片左右移动。
mCamera.translate(0.0f, 0.0f, -20.0f);
if(rotation <mMaxRotationAngle){
float zoomAmount = (float)(mMaxZoom+(rotation*1.0));
mCamera.translate(0.0f, 0.0f, zoomAmount);
}
mCamera.rotateY(rotationAngle);// rotationAngle 为正,沿y轴向内旋转; 为负,沿y轴向外旋转
mCamera.getMatrix(imageMatrix);
imageMatrix.preTranslate(-(imageWidth/2), -(imageHeight/2));
imageMatrix.postTranslate((imageWidth/2), (imageHeight/2));
mCamera.restore();
}
}
代码没什么好说的,布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="16sp" />
<picturegame.view.GalleryView
android:id="@+id/myGallery"
android:spacing="20dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:unselectedAlpha="128"
android:layout_below="@id/tv_name"
android:layout_marginTop="10dip" />
</RelativeLayout>
四、问题分析
按理说图片应该是在屏幕中间的呀,可是为什么没有呢?找了好久,最后发现问题出在这里:平时自定义view时,第二个构造函数的第三个参数默认都是写0,所以这就导致了Gallery中元素没有居中的问题。
看一眼Gallery中源码是如何写的:
public Gallery(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.galleryStyle);
}
没错,就是这里,我们发现第三个参数是R.attr.galleryStyle,因此如果第三个参数写0,就会使它失效。
它和一般的view不一样的,如view中是:
public View(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
surfaceview中是:
public SurfaceView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
五、解决问题
我们将自定义Gallery第二个构造函数改一下:
public GalleryView(Context context,AttributeSet attrs) {
// TODO Auto-generated constructor stub
//this(context,attrs,0);
super(context, attrs);
this.setStaticTransformationsEnabled(true);
}
看看运行效果:
六、总结
希望可以帮助到大家,同时记录于此提醒自己。