Android 自定义Galley中图片未居中显示问题
来源:互联网 发布:数据恢复案例视频教程 编辑:程序博客网 时间:2024/04/29 11:38
一、引言
最近复习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 stubthis(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 stubthis.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;}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {// TODO Auto-generated method stubmCoveflowCenter=getCenterOfCoverflow();super.onSizeChanged(w, h, oldw, oldh);}@Overrideprotected boolean getChildStaticTransformation(View child, Transformation trans) {// TODO Auto-generated method stubfinal int childCenter = getCenterOfView(child);final int childWidth = child.getWidth();int rotationAngle = 0;trans.clear();trans.setTransformationType(Transformation.TYPE_BOTH); //透明度和比例都要变换if(childCenter == mCoveflowCenter){//中间的viewtransformImageBitmap((ImageView)child, trans, 0);}else{//两侧的viewrotationAngle = (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);}看看运行效果:
六、总结
希望可以帮助到大家,同时记录于此提醒自己。
0 0
- Android 自定义Galley中图片未居中显示问题
- android galley图片轮播
- Android 之 Galley, 手指滑动翻看图片
- Android自定义TextView实现文字图片居中显示
- Android中Galley视图的运用
- Android中图片的展示之Webview居中显示图片
- Android RadioButton左边图片不能居中显示问题
- 兼容小问题之在IE中图片未显示
- android 自定义RadioButton 实现图标居中显示
- Android自定义控件文本居中显示
- Android怎么让RadioButton图片居中显示
- Android DrawableTextView图片文字居中显示
- 设置滑动视图中图片居中显示
- HTML中使图片居中显示
- vue 中图片居中/置顶显示
- android galley实现画廊效果
- RadioGroup没有文字只显示图片,图片居中问题
- css 图片在div中居中问题
- jfinalQ开发教程09-qiao-util.jar:其他工具
- 八皇后算法
- Fourier Transform Intro - Fourier Integral Theorem
- jfinalQ开发教程10-qiao.js:js工具和bootstrap封装
- 雅虎UI 取色器 【笨笨 20151005】
- Android 自定义Galley中图片未居中显示问题
- Hadoop2.7.1——NFS部署
- 动软代码生成器v2.78Mysql备注生成完美修复dll
- IDM破解实录
- storm初识
- 万能的meta,属性详解
- CISCO学习笔记(三)交换机的基本原理及vlan和vlan间路由
- Eclipse错误:Conversion to Dalvik format failed with error 1
- 论assign copy retain readonly readwrite nonatomic weak strong各自的特点