【发布时间】:2010-09-03 06:00:12
【问题描述】:
我是 android 开发的新手。现在我想做如下图所示的圆形画廊视图。事情是当用户从左到右和从右到左滚动时我想放大中心图像。有这方面的教程吗?
我想要的是被刷过的图像在中心时需要放大。我想我可以用画廊做到这一点。但来自 android 开发人员的示例不是我想要的。 :(
【问题讨论】:
标签: android android-gallery android-image
我是 android 开发的新手。现在我想做如下图所示的圆形画廊视图。事情是当用户从左到右和从右到左滚动时我想放大中心图像。有这方面的教程吗?
我想要的是被刷过的图像在中心时需要放大。我想我可以用画廊做到这一点。但来自 android 开发人员的示例不是我想要的。 :(
【问题讨论】:
标签: android android-gallery android-image
你可以试试:
public class TestGallery extends Activity {
/** Called when the activity is first created. */
private Integer[] mImageIds = { R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4 };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
if (position >= mImageIds.length) {
position = position % mImageIds.length;
}
Toast.makeText(TestGallery.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return Integer.MAX_VALUE;
}
public Object getItem(int position) {
if (position >= mImageIds.length) {
position = position % mImageIds.length;
}
return position;
}
public long getItemId(int position) {
if (position >= mImageIds.length) {
position = position % mImageIds.length;
}
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
if (position >= mImageIds.length) {
position = position % mImageIds.length;
}
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(80, 80));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
public int checkPosition(int position) {
if (position >= mImageIds.length) {
position = position % mImageIds.length;
}
return position;
}
}}
【讨论】:
(Integer.MAX_VALUE/2 + N)(Integer.MAX_VALUE/2 + N) % array.lenght == 0,0 < N <array.lenght,你会得到一个漂亮的圆形画廊(两个方向)。
如果要放大中心选定的图像,有一种可能的方法。
在您的 onItemSelected 方法上,只需调用动画来缩放对象。画廊的特性是它始终是中心锁定的。因此将始终选择中心元素。
希望这会奏效..
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
android:fillAfter="true"
>
<scale
android:fromXScale="1.0"
android:toXScale="1.50"
android:fromYScale="1.0"
android:toYScale="1.50"
android:duration="600"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="true"/>
</set>
请记住,您必须存储上一个视图,因为当元素从中心移开时,它应该被放置为正常大小。
因此您可以有两个视图 - prevView 和 currView。
在 currView 上做动画。
谢谢,
森
【讨论】:
我为此创建了自己的教程: http://evgeni-shafran.blogspot.com/2011/08/tutorial-custom-gallery-circular-and.html
要让它成为循环,你需要让它认为它有很多项目,比你真正拥有的要多得多。
然后通过设置 position= position % items.length 您可以创建类似的内容(我将展示 3 个项目):1,2,3,1,2,3,1,2,3,1,2, 3,1,2,3,1,2,3,1,2,3 然后走到中间,这样即使卷轴很多他也不会接近结尾。 1,2,3,1,2,3,1,2,3,->1
要选择它:您需要覆盖 setOnItemSelectedListener 并操纵大小。不要忘记保存对上一个视图的引用,这样当您进入下一个视图时,您可以使其看起来很规则,而不是放大。
我在上面列出的教程中实现了这两个
【讨论】: