Android 以画廊风格滚动应用内图像

Posted

技术标签:

【中文标题】Android 以画廊风格滚动应用内图像【英文标题】:Android scroll through in-app images in a gallery style way 【发布时间】:2012-07-14 14:48:54 【问题描述】:

我有一个图像的 GridView,它们都在 /res 目录中(随应用程序提供)。当我打开其中一个时,没有问题。但是,我希望能够以与在图库中相同的方式在它们之间滚动,即在图像上滑动手指将导致下一个图像出现。有没有办法做到这一点? 谢谢!

【问题讨论】:

android viewPager implementation的可能重复 【参考方案1】:

您可以使用ViewPager

请参阅android viewPager implementation 获取一些很好的链接来了解如何实现它。

【讨论】:

【参考方案2】:

我最终创建了一个我自己的简单实现:

public class PicView extends View

private int mBackgroundPicPosition;
private Bitmap mBackgroundPic;
private int m_touchStartPosX;
private EventsActivity m_mainActivity;
private int m_viewWidth;
private int m_viewHeight;
private int m_backgroundX;
private Integer[] m_picIDs;

public PicView(Context context, Integer[] picIDs) 
    super(context);
    m_mainActivity = (EventsActivity)context;
    m_viewWidth = m_mainActivity.mMetrics.widthPixels;
    m_viewHeight = m_mainActivity.mMetrics.heightPixels;
    m_picIDs = picIDs;

public void setBackground(int position) 
    Options opts = new Options();
    mBackgroundPicPosition = position;
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), m_picIDs[position], opts);
    mBackgroundPic = BitmapFactory.decodeResource(getResources(), m_picIDs[position], opts);

    int picHeight = bitmap.getHeight();
    int picWidth = bitmap.getWidth();

    mBackgroundPic.recycle();
    float xScale, yScale, scale;
    if (picWidth > picHeight) 
        // rotate the picture
        Matrix matrix = new Matrix();
        matrix.postRotate(-90);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        picHeight = bitmap.getHeight();
        picWidth = bitmap.getWidth();
        matrix = null;  

     
    xScale = ((float)m_viewWidth)/ picWidth;     
    yScale = ((float)m_viewHeight) / picHeight;
    scale = (xScale <= yScale) ? xScale : yScale;
    m_backgroundX = (xScale >= yScale) ? (m_viewWidth - (int)(picWidth * scale)) / 2 : 0;
    mBackgroundPic = Bitmap.createScaledBitmap(bitmap, (int)(picWidth * scale), (int)(picHeight * scale), true);
    bitmap = null;

    invalidate();


@Override
protected void onDraw(Canvas canvas) 
    // draw the background
    canvas.drawBitmap(mBackgroundPic, m_backgroundX, 0, null);


@Override
public boolean onTouchEvent(MotionEvent event) 
    int eventaction = event.getAction();
    int X = (int)event.getX();

    switch (eventaction ) 
        case MotionEvent.ACTION_DOWN:
            m_touchStartPosX = X;
        break;
        case MotionEvent.ACTION_UP:
            //check to see if sliding picture
            if (X <= m_touchStartPosX) 
                // slide to the left
                setBackground(getNextPosition());
             else 
                // slide to the right
                setBackground(getPrevPosition());
            
            m_touchStartPosX = -1;  
        break;
    
    invalidate();  
    return true;



private int getPrevPosition() 
    if (mBackgroundPicPosition == 0) 
        return m_picIDs.length - 1;
     else 
        return mBackgroundPicPosition - 1;
    


private int getNextPosition() 
    if (mBackgroundPicPosition == m_picIDs.length - 1) 
        return 0;
     else 
        return mBackgroundPicPosition + 1;
    

【讨论】:

以上是关于Android 以画廊风格滚动应用内图像的主要内容,如果未能解决你的问题,请参考以下文章

滑动幻灯片内图像周围的奇怪灰色边框

Xamarin Android 应用程序内图标上数字提示

android,设计画廊滚动条,三张图片,一张图片占1/3滚动条,如图所示,最好使用HorizontalScrollView

创建 Trover 样式库 iOS Swift

Android - 如何制作可滚动的约束布局?

如何获得关于画廊应用程序可见的每个新图像的通知?