Android 圆形画廊?

发布于 2024-09-16 19:14:07 字数 211 浏览 4 评论 0原文

我是android开发的新手。现在我想做如下图所示的圆形图库视图。事情是,当用户从左到右和从右到左滚动时,我想放大中心图像。有相关教程吗?

在此处输入图像描述 我想要的是被滑动的图像在中心时需要被放大。我想我可以用画廊来做到这一点。但 android 开发人员提供的示例不是我想要的。 :(

I am a newbie to android development.Now i would like to do gallery view as circular like image as below.The things is that i want to enlarge the center image when user scroll from left to right and right to left. Is there any tutorials for that ?

enter image description here
what I want is the image that's been swiped need to be enlarged while it's at the center. I thought I could do it with Gallery. but the example from the android developer is not the one I want. :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小姐丶请自重 2024-09-23 19:14:07

你可以尝试:

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; 
    } 
}}

youcan try:

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; 
    } 
}}
忆伤 2024-09-23 19:14:07

如果您想放大中心选定的图像,有一种可能的方法。
在 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 上做动画。

谢谢,

If you want to enlarge the center selected image there is one possible way.
On your onItemSelected method, just call an animation to zoom the object. The property of gallery is that it is always center-locked. So the center element will be always selected.
Hope that will work..

<?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>

Do remember that you will have to store the previous view as when the element is move away from center it should be put to the normal size.

So you can have two views - prevView and currView.
Do the animation on the currView.

Thanks,
Sen

将军与妓 2024-09-23 19:14:07

我为此创建了自己的教程:
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<-,2,3,1,2,3,1,2,3,1 ,2,3

要选择它:您需要重写 setOnItemSelectedListener 并操作大小。不要忘记保存对上一个视图的引用,这样当您到达下一个视图时,您可以使其看起来正常,而不是放大。

我在上面列出的教程中实现了这两个功能

I created my own tutorial for this:
http://evgeni-shafran.blogspot.com/2011/08/tutorial-custom-gallery-circular-and.html

For it to be circular you need to make it think that it have A LOT of items, a lot more then you really have.

And then by making position= position % items.length you create something like (I will show it for 3 items) : 1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3
And then go to the middle so even if the scroll a lot he wont come near to the end.
1,2,3,1,2,3,1,2,3,->1<-,2,3,1,2,3,1,2,3,1,2,3

For it to be selected: you need to override setOnItemSelectedListener and manipulate the size. dont forget to save a reference to your last view so when you get to the next one you can make it look regular, not enlarged.

I implemented both of this in my tutorial listed above

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文