android使用左右滑动更改图像

时间:2017-08-23 10:37:36

标签: android uiimageview

我正在创建一个Android应用程序,每当应用程序运行时,会出现一个包含多个组件的活动屏幕。现在,此活动有几个不同的组件。其中一个是图像视图,显示来自图库的不同图像。目前,这些图像通过触摸手势改变。但我想使用左右滑动来实现相同的功能。这是我的代码:

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;


import java.util.Collection;

public class pictures extends ContentResolverAnimatedViewContainer 
{
    public static final String IMAGE_NAME = Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera";
    public static final String IMAGE_ID = getBucketId(CAMERA_IMAGE_BUCKET_NAME);
    public static String getBucketId(String path) 
    {
        return String.valueOf(path.toLowerCase().hashCode());   }
    static Context cc;
    String cp;
    int picheight;

    public PhotoExample(final Context context) {
        super(context, 9);
        cc = context;
        picheight = getResources().getDimensionPixelSize(R.dimen.photo_example_height);
        final String[] projection = { Images.Media.DATA };
        final String selection = Images.Media.BUCKET_ID + " = ?";
        final String[] selectionArgs = { IMAGE_ID };
        createContentResolverLoop(Images.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, null);   }
    @Override
    public void onCreateData(ContentResolver contentResolver, Cursor dataCursor) {
        final int column_data = dataCursor.getColumnIndexOrThrow(Images.Media.DATA);
        cp = dataCursor.getString(column_data); }

    @Override
    public void onCreateViewContent(LayoutInflater layoutInflater, ViewGroup parentGroup, View[] containerViews, int index) {
        containerViews[index] = layoutInflater.inflate(R.layout.example_photo, parentGroup, false);
        ImageView photoView = (ImageView)containerViews[index].findViewById(R.id.photo);
        photoView.setImageBitmap(decodeSampledBitmapFromFile(cp, 0, picheight));    }

    public static Bitmap decodeSampledBitmapFromFile(String path, int Width, int Height) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        options.Size = calculateInSampleSize(options, Width, Height);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(path, options);}

    public static int calculateInSampleSize(BitmapFactory.Options options, int Width, int Height) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int Size = 1;

        if (height > reqHeight || width > reqWidth) {
            final int height_half = height / 2;
            final int width_half = width / 2;

            while ((height_half / Size) > Height && (width_half / Size) > Width) {
                Size = Size * 2;
            }    }

        return Size;
    }


    @Override
    public void cleanup() { }

}

任何想法我应该如何实施?

感谢。

2 个答案:

答案 0 :(得分:3)

 [INFO] Apache Oozie Core .................................. FAILURE [ 52.316 s]
    [INFO] Apache Oozie Share Lib Streaming ................... SKIPPED
    [INFO] Apache Oozie Share Lib Pig ......................... SKIPPED
    [INFO] Apache Oozie Share Lib Hive ........................ SKIPPED
    [INFO] Apache Oozie Share Lib Hive 2 ...................... SKIPPED
    [INFO] Apache Oozie Share Lib Sqoop ....................... SKIPPED
    [INFO] Apache Oozie Examples .............................. SKIPPED
    [INFO] Apache Oozie Share Lib Spark ....................... SKIPPED
    [INFO] Apache Oozie Share Lib ............................. SKIPPED
    [INFO] Apache Oozie Docs .................................. SKIPPED
    [INFO] Apache Oozie WebApp ................................ SKIPPED
    [INFO] Apache Oozie Tools ................................. SKIPPED
    [INFO] Apache Oozie MiniOozie ............................. SKIPPED
    [INFO] Apache Oozie Distro ................................ SKIPPED
    [INFO] Apache Oozie ZooKeeper Security Tests .............. SKIPPED
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 01:58 min
    [INFO] Finished at: 2017-08-23T15:45:13+05:30
    [INFO] Final Memory: 179M/818M
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:testCompile (default-testCompile) on project oozie-core: Compilation failure: Compilation failure:
    [ERROR] /home/azhar/Downloads/oozie-4.3.0/core/src/test/java/org/apache/oozie/service/TestEventHandlerService.java:[213,46] error: cannot find symbol
    [ERROR] variable logEntry of type LoggingEvent
    [ERROR] /home/azhar/Downloads/oozie-4.3.0/core/src/test/java/org/apache/oozie/service/TestEventHandlerService.java:[214,31] error: cannot find symbol
    [ERROR] variable logEntry of type LoggingEvent
    [ERROR] /home/azhar/Downloads/oozie-4.3.0/core/src/test/java/org/apache/oozie/service/TestEventHandlerService.java:[215,81] error: cannot find symbol
    [ERROR] variable logEntry of type LoggingEvent
    [ERROR] /home/azhar/Downloads/oozie-4.3.0/core/src/test/java/org/apache/oozie/service/TestEventHandlerService.java:[221,46] error: cannot find symbol
    [ERROR] variable logEntry of type LoggingEvent
    [ERROR] /home/azhar/Downloads/oozie-4.3.0/core/src/test/java/org/apache/oozie/service/TestEventHandlerService.java:[222,31] error: cannot find symbol
    [ERROR] variable logEntry of type LoggingEvent
    [ERROR] /home/azhar/Downloads/oozie-4.3.0/core/src/test/java/org/apache/oozie/service/TestEventHandlerService.java:[231,31] error: cannot find symbol
    [ERROR] variable logEntry of type LoggingEvent
    [ERROR] /home/azhar/Downloads/oozie-4.3.0/core/src/test/java/org/apache/oozie/service/TestEventHandlerService.java:[239,31] error: cannot find symbol
    [ERROR] variable logEntry of type LoggingEvent
    [ERROR] /home/azhar/Downloads/oozie-4.3.0/core/src/test/java/org/apache/oozie/sla/TestSLACalculatorMemory.java:[817,47] error: cannot find symbol
    [ERROR] variable firstLogEntry of type LoggingEvent
    [ERROR] /home/azhar/Downloads/oozie-4.3.0/core/src/test/java/org/apache/oozie/sla/TestSLACalculatorMemory.java:[818,32] error: cannot find symbol
    [ERROR] variable firstLogEntry of type LoggingEvent
    [ERROR] /home/azhar/Downloads/oozie-4.3.0/core/src/test/java/org/apache/oozie/sla/TestSLACalculatorMemory.java:[819,78] error: cannot find symbol
    [ERROR] -> [Help 1]
    org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:testCompile (default-testCompile) on project oozie-core: Compilation failure
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
    Caused by: org.apache.maven.plugin.CompilationFailureException: Compilation failure
        at org.apache.maven.plugin.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:656)
        at org.apache.maven.plugin.TestCompilerMojo.execute(TestCompilerMojo.java:161)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
        ... 20 more
    [ERROR] 
    [ERROR] 
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
...

    ERROR, Oozie distro creation failed

然后使用:

import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector;

public OnSwipeTouchListener (Context ctx){
    gestureDetector = new GestureDetector(ctx, new GestureListener());
}

private final class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
                result = true;
            } 
            else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                }
                result = true;

        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeTop() {
}

public void onSwipeBottom() {
}
}

答案 1 :(得分:1)

使用许多类似Tinder的可打开卡片库中的一个。 DuckDuckGo is your friend (or Google).

一些例子:

您有很多选择可供选择。