如何实现背景运动效果(视差效果)

时间:2019-05-15 04:52:41

标签: android motion-effect

在大多数设备上,可以启用“墙纸的运动效果”,我的问题是是否可以在“活动”背景上设置此效果?如果是的话,我该怎么办?

  

Motion Effect在智能手机上,它只是一种效果,可以根据手机上的动作进行移动。如果向右倾斜手机,您会注意到墙纸会产生使它看起来也向右倾斜的效果。

MotionGif

2 个答案:

答案 0 :(得分:2)

您可以在给定的库下面使用。

将此库添加到gradle文件中。

implementation 'com.gjiazhe:PanoramaImageView:1.0'

使用下面的PanoramaImageView代替ImageView,您将获得解决方案。

<com.gjiazhe.panoramaimageview.PanoramaImageView
     android:id="@+id/panorama_image_view"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:src="@drawable/img"
     app:piv_enablePanoramaMode="true"
     app:piv_show_scrollbar="true"
     app:piv_invertScrollDirection="false" />

要了解更多信息,请访问我的site

例如:

enter image description here enter image description here

答案 1 :(得分:1)

根据Parallax ImageView Android library创建自定义ImageView:

super

按如下所示使用它:

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

public class ParallaxImageView extends AppCompatImageView implements SensorEventListener {
    float[] rotMat = new float[16];
    float[] vals = new float[3];
    //sensor parallax effect
    private SensorManager senSensorManager;
    private Sensor senAccelerometer;
    private int sideVerticalMargin, sideHorizontalMargin;
    private float verticalMultiplier=1, horizontalMultiplier=1;

    public ParallaxImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

        senSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
        senSensorManager.registerListener(this, senAccelerometer , SensorManager.SENSOR_DELAY_FASTEST);

    }

    public ParallaxImageView(Context context) {
        super(context);
    }

    public void setMargins(int VM, int HM){
        this.sideVerticalMargin = -VM;
        this.sideHorizontalMargin = -HM;
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)this.getLayoutParams();
        params.setMargins(-HM, -VM, -HM, -VM);
        this.setLayoutParams(params);
    }

    public void setMultipliers(float Vertical, float Horizontal){
        this.verticalMultiplier = Vertical;
        this.horizontalMultiplier = Horizontal;
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        Sensor mySensor = event.sensor;

        if (mySensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {

            // Convert the rotation-vector to a 4x4 matrix.
            try {
                SensorManager.getRotationMatrixFromVector(rotMat, event.values);
            } catch (IllegalArgumentException e) {
                if (event.values.length > 3) {
                    // Note 3 bug
                    float[] newVector = new float[] {
                            event.values[0],
                            event.values[1],
                            event.values[2]
                    };
                    SensorManager.getRotationMatrixFromVector(rotMat, newVector);
                }
            }
            SensorManager.remapCoordinateSystem(rotMat,
                    SensorManager.AXIS_Y, SensorManager.AXIS_X,
                    rotMat);

            SensorManager.getOrientation(rotMat, vals);

            vals[0] = (float) Math.toDegrees(vals[0]);
            vals[1] = (float) Math.toDegrees(vals[1]);
            vals[2] = (float) Math.toDegrees(vals[2]);

            int leftfloat = (int) (this.sideHorizontalMargin-(vals[1]*this.horizontalMultiplier));

            int topfloat;

            if(vals[2]>0){
                topfloat=(int) (this.sideVerticalMargin+(vals[2]*this.verticalMultiplier));
            }else{
                topfloat=(int) (this.sideVerticalMargin-(vals[2]*this.verticalMultiplier));
            }

            this.setX(leftfloat);
            this.setY(topfloat);
        }

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    public void onPause() {
        senSensorManager.unregisterListener(this);
    }
    public void onResume() {
        senSensorManager.registerListener(this, senAccelerometer, SensorManager.SENSOR_DELAY_FASTEST);
    }
}

在活动中:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="0dip"
    tools:context="com.example.sensortest.MainActivity" >

    <ir.tcharter.testapp.ParallaxImageView
        android:id="@+id/iv_background"
        android:src="@drawable/background"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"/>

</RelativeLayout>

不要忘记将图像添加到drawable中。在这里添加了background.png。