带有滑动图片的Android Gallery应用程序



如标题中。我想拥有由2列组成的图片网格,每个图片都必须具有相同的尺寸。用户应该能够单击每张图片以进入全屏幕模式,然后从左到右滑动图片(在该模式下全尺寸的图片)。到目前为止,我制作了可单击的元素的网格,但没有滑动功能。这是我的代码:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.miki.androidtwo.MainActivity">
    <GridView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="2"
        android:scrollbars="vertical"
        android:gravity="center">
    </GridView>
</RelativeLayout>

activity_full_image.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    tools:context="com.example.miki.androidtwo.FullImageActivity">
    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="I want to see other doggos" />
</RelativeLayout>

mainActivity.java

public class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GridView gridView = (GridView) findViewById(R.id.grid);
        // Instance of ImageAdapter Class
        gridView.setAdapter(new ImageAdapter(this));
        /**
         * On Click event for Single Gridview Item
         * */
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                // Sending image id to FullScreenActivity
                Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
                // passing array index
                i.putExtra("id", position);
                startActivity(i);
            }
        });
    }
}

其他类别是充分的immageeeeeeyitivity and ImageDapter;它们非常明显且简单,所以我不会发布它们。

  1. 如何实现此滑动图片功能?

  2. 如何确定将独立于设备的网格中图片的尺寸?实际上,我认为我用该行进行了硬编码:

    imageView.setLayoutParams(new GridView.LayoutParams(480,402));

如果您想在图像不是全屏幕时滑动图像,则可以使用HorizonTalsCrollview,或者如果您想在完整的屏幕上滑动它们,则可以使用视图器,沿使用Pageradapter。

因此,要填充网格imageView.setLayoutParams,可以像您一样设置每个图像视图的大小。当用户单击图像时,您将转到使用ViewPager和Pageradapter设置的新活动。这样,用户可以通过全屏图像滑动。

最新更新