【问题标题】:Swipe Image with button and fingers用按钮和手指滑动图像
【发布时间】:2011-10-23 21:09:07
【问题描述】:

我正在尝试使用以全屏模式显示的图像创建一个自定义画廊,并且能够使用“上一个下一个”按钮和手指在不同图像之间滑动。所以现在我用手指更改图像来完成该部分,但我有重新设计我的 xml 文件以及我的代码的整个结构和逻辑,我需要一些关于如何添加新东西的帮助或建议。所以我用来用手指滑动图像的旧代码是这样的:

  this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        HorizontalPager realViewSwitcher = new HorizontalPager(getApplicationContext());

        ImageView img1 = new ImageView(getApplicationContext());
        ImageView img2 = new ImageView(getApplicationContext());
        ImageView img3 = new ImageView(getApplicationContext());
        ImageView img4 = new ImageView(getApplicationContext());
        ImageView img5 = new ImageView(getApplicationContext());
        ImageView img6 = new ImageView(getApplicationContext());

        img1.setImageResource(R.drawable.one);
        img2.setImageResource(R.drawable.two);
        img3.setImageResource(R.drawable.three);
        img4.setImageResource(R.drawable.four);
        img5.setImageResource(R.drawable.five);
        img6.setImageResource(R.drawable.six);

        realViewSwitcher.addView(img1);
        realViewSwitcher.addView(img2);
        realViewSwitcher.addView(img3);
        realViewSwitcher.addView(img4);
        realViewSwitcher.addView(img5);
        realViewSwitcher.addView(img6);

        setContentView(realViewSwitcher);

这段代码以全屏模式显示图像,我可以在它们之间滑动。现在我需要做这样的事情:

我希望能够用按钮和手指滑动图像。我的第二个问题是如何隐藏顶部的栏(Back、Galler、Info)和底部的栏(上一个、下一个)并显示图像在全屏模式下,仍然只能用手指滑动图像。 这是我的 xml 文件的外观:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" >

            <ImageView 
                android:id="@+id/single_card"
                android:src="@drawable/one"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <RelativeLayout
                android:id="@+id/actionbar"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#000000"
                android:orientation="horizontal"
                android:layout_gravity="center"
                android:padding="10dp"
                android:layout_alignParentTop="true" >

                <Button
                    android:id="@+id/back_button"
                    android:text="Back"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:background="#333333"
                    android:layout_centerVertical="true"
                    android:textSize="13dp"
                    android:textColor="#ffffff"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />

                <TextView 
                    android:text="Gallery"
                    android:textStyle="bold"
                    android:textSize="15dp"
                    android:textColor="#FFFFFF"
                    android:id="@+id/single_msg_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" 
                    android:layout_centerInParent="true"/>

                <Button
                    android:id="@+id/info_button"
                    android:text="Info"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:background="#333333"
                    android:layout_centerVertical="true"
                    android:textSize="13dp"
                    android:textColor="#ffffff"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp" />

            </RelativeLayout>


            <RelativeLayout 
                android:id="@+id/content"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_alignParentBottom="true"
                android:background="#000000" >


                <Button
                    android:id="@+id/previous_button"
                    android:text="Previous"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#333333"
                    android:layout_alignParentLeft="true"
                    android:textSize="13dp"
                    android:textColor="#ffffff"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:layout_marginLeft="50dp"
                    android:layout_marginBottom="10dp"
                    android:layout_alignParentBottom="true" />

                <Button
                    android:id="@+id/next_button"
                    android:text="Next"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="#333333"
                    android:layout_alignParentRight="true"
                    android:textSize="13dp"
                    android:textColor="#ffffff"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:layout_marginRight="50dp"
                    android:layout_marginBottom="10dp"
                    android:layout_alignParentBottom="true" />

            </RelativeLayout>


</RelativeLayout>

提前致谢!!!欢迎任何帮助、建议或示例链接!

【问题讨论】:

    标签: android image gallery fullscreen swipe


    【解决方案1】:

    我前段时间做过类似的事情,基本上手指向左滑动或手指向右滑动执行 2 个不同的操作,并且有相同操作的按钮可以单击

    在我的主要活动中,我注册了手指滑动监听器:

    gestureDetector = new GestureDetector(new ListItemGestureDetector(this));
    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)) {
                return true;
            }
            return false;
        }
    };
    

    然后在呈现我的列表视图的高效适配器中,我注册按钮及其点击(这对你来说不一样,但是当我将按钮添加到我的布局时我这样做(呼叫和短信是我布局中的按钮)

    static class ViewHolder {
        TextView name, phone;
        ImageButton call, sms, other;
        //ImageView icon;
    }
    
        ViewHolder holder = new ViewHolder();
        holder.call.setClickable(true);
        holder.call.setFocusable(true);
        holder.call.setTag("call");
        final int pos = position;
    
        holder.call.setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View view) {
                Toast.makeText(context, "CALL", Toast.LENGTH_SHORT).show();
                Cursor c = (Cursor) getItem(pos);
                Intent i = new Intent(Intent.ACTION_CALL);
                String phoneNumber = c.getString(WaddleAddressBook.DATA_ID);
                i.setData(Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, phoneNumber));
                context.startActivity(i);
            }
        });
    

    【讨论】:

    • 实际上我不明白如何用你的代码更改我的代码,以便让事情正常工作。
    • 在您的活动中,注册手势检测器。然后使用其中的按钮获取您的相对布局,并在其中为每个按钮附加点击侦听器。
    【解决方案2】:

    使用视图 Flow 项目来执行此操作。您添加 imageViews 的代码根本不是一个好的解决方案。

    在 gitHub 上查看 viewFlow。也有带有图像的示例。你所做的就是在活动中添加这个布局,制作一个图像适配器,就是这样。

    【讨论】:

      【解决方案3】:

      对于滑动图像,我们可以简单地使用 ViewPager 概念。

      1.在布局中设置 viewpager

          <android.support.v4.view.ViewPager
          android:id="@+id/view_pager"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" />
          </RelativeLayout>
      

      2.MyMain.java

                      public class MyMain extends Activity {
                            @Override
                          public void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.main);
      
                       ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
                        ImagePagerAdapter adapter = new ImagePagerAdapter();
                        viewPager.setAdapter(adapter);
                     }
      
                  private class ImagePagerAdapter extends PagerAdapter {
                       private int[] mImages = new int[] {
                             R.drawable.one,
                             R.drawable.two,
                             R.drawable.hree,
                             R.drawable.four
                                 };
      
                               @Override
                             public int getCount() {
                             return mImages.length;
                               }
      
                   @Override
                     public boolean isViewFromObject(View view, Object object) {
                        return view == ((ImageView) object);
                           }
      
                     @Override
                     public Object instantiateItem(ViewGroup container, int position) {
                          Context context = MyMain.this;
                        ImageView imageView = new ImageView(context);
                     int padding =context.getResources().  
                          getDimensionPixelSize(R.dimen.padding_medium);
                       imageView.setPadding(padding, padding, padding, padding);
                       imageView.setScaleType(ImageView.ScaleType.CENTER);
            imageView.setImageResource(mImages[position]);
            ((ViewPager) container).addView(imageView, 0); 
            return imageView;
          }
      
          @Override
          public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((ImageView) object);
          }
            }
      
      
          }
      

      【讨论】:

        猜你喜欢
        • 2013-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多