开发者

Android: How to implement moving to the next ImageView by doing quick slide vertically? (All Source Codes Provided)

I have about 20 images. I have created buttons of each image and one ImageView. When a button clicked, the corresponding image will set to ImageView with enabled zooming and dragging. Then, replace it upon clicking the other button to move to the next/previous image.

Now, I have to remove the buttons and replace it by implementing quick slide vertically. When I do quick slide from bottom to top, it will move to the next image. Then quick slide from top to bottom to move to the previous image. Also disable it if they reach first im开发者_如何转开发age or last image.

I am thinking of List of ImageView to handle the images. Then, implement quick sliding function by maintaining an index of the current ImageView on the List.

Any guidance on the right direction on how to implement the quick slide paging using Android 2.1 is highly appreciated.

UPDATES1 I successfully use the ImageSwitcher slide up and down using a button (Next, Prev). My next problem is on how to implement it by quick slide to move Next&Prev. I will add my current codes.

NOTE: TouchImageView class supports Zooming and Dragging. When you slide slowly, image will drag to that direction but when I do quick slide up or down want to move to Next & Previous image respoectively.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ImageSwitcher android:id="@+id/switcher1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_weight="0"
    />
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
        <Button android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next"/>
        <Button android:id="@+id/previous"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Prev"/>
    </LinearLayout>

</LinearLayout>

Activity: (Note: TouchImageView gotten from RecipeBook060 Zoom and Drag Extended ImageView, Slide Images idea gotten from Slide Image)

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ViewSwitcher.ViewFactory;

public class ImageSwticherActivity extends Activity 
    implements ViewFactory {
    Integer[] imageIDs = { R.drawable.image_one, R.drawable.image_two,
            R.drawable.image_three };

    private ImageSwitcher imageSwitcher;
    private Button nextButton;
    private Button previousButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Animation in = AnimationUtils.loadAnimation(this,
                android.R.anim.slide_in_left);
        final Animation out = AnimationUtils.loadAnimation(this,
                android.R.anim.slide_out_right);

        imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1);
        imageSwitcher.setFactory(this);
        imageSwitcher.setInAnimation(in);
        imageSwitcher.setOutAnimation(out);

        imageSwitcher.setImageResource(imageIDs[0]);     
        nextButton = (Button) findViewById(R.id.next);    
        nextButton.setOnClickListener(new OnClickListener() {       
            public void onClick(View v) {  
                Animation out= AnimationUtils.loadAnimation(ImageSwticherActivity.this, R.anim.slide_out_bottom);     
                Animation in= AnimationUtils.loadAnimation(ImageSwticherActivity.this, R.anim.slide_in_top);   

                imageSwitcher.setInAnimation(in);    
                imageSwitcher.setOutAnimation(out);        
                imageSwitcher.setImageResource(imageIDs[1]);     
            }    
        });   

        previousButton = (Button) findViewById(R.id.previous);     
        previousButton.setOnClickListener(new OnClickListener() { 
            public void onClick(View v) {  
                Animation out= AnimationUtils.loadAnimation(ImageSwticherActivity.this, R.anim.slide_out_top);     
                Animation in= AnimationUtils.loadAnimation(ImageSwticherActivity.this, R.anim.slide_in_bottom);   
                imageSwitcher.setInAnimation(in);    
                imageSwitcher.setOutAnimation(out);  
                imageSwitcher.setImageResource(imageIDs[0]);     
            }   
        }); 

    }

    @Override
    public View makeView() {
        //ImageView imageView = new ImageView(ImageSwticherActivity.this);
        TouchImageView imageView = new TouchImageView(ImageSwticherActivity.this);
        imageView.setBackgroundColor(0xFF000000);
        //imageView.setScaleType(ImageView.ScaleType.CENTER);
        imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
                ImageSwitcher.LayoutParams.FILL_PARENT, ImageSwitcher.LayoutParams.FILL_PARENT));
        return imageView;
    }

}

slide_in_bottom.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromYDelta="-50%p" android:toYDelta="0"
            android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

slide_in_top.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromYDelta="50%p" android:toYDelta="0"
            android:duration="@android:integer/config_mediumAnimTime"/>
        <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

slide_out_bottom.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromYDelta="0" android:toYDelta="-50%p"
            android:duration="@android:integer/config_mediumAnimTime"/>
        <alpha android:fromAlpha="1.0" android:toAlpha="0.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

slide_out_top.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromYDelta="0" android:toYDelta="50%p"
            android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜