【问题标题】:Barcode animation, port to ConstraintLayout条码动画,移植到 ConstraintLayout
【发布时间】:2020-04-30 16:05:49
【问题描述】:

我发现此代码可以为红色条(条形码扫描仪)设置动画。然而,它是为具有固定尺寸的相对布局而设计的。 我需要找到一个浮点值(相对于容器)而不是一个固定值。

这里是布局:

    <RelativeLayout
        android:id="@+id/trackBox"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerInParent="true"
        android:background="@drawable/background_border"
        app:layout_constraintBottom_toTopOf="@+id/guidelineBarcodeScanner04"
        app:layout_constraintEnd_toStartOf="@+id/guidelineBarcodeScanner01"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="@+id/guidelineBarcodeScanner02"
        app:layout_constraintTop_toTopOf="@+id/guidelineBarcodeScanner03"
        app:layout_constraintVertical_bias="0.0">

        <RelativeLayout
            android:id="@+id/midLine"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#FF0000" />

    </RelativeLayout>

其中midLine为红色条,trackBox为白色边框,条需要上下滑动。

这里是实际代码:

    public void slideToAbove() {

        Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, -300);
        slide.setDuration(2000);

        midLine.startAnimation(slide);

        slide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                midLine.clearAnimation();
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                        midLine.getWidth(), midLine.getHeight());
                lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                midLine.setLayoutParams(lp);

                slideToDown();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }

    public void slideToDown() {

        Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, 300);
        slide.setDuration(2000);

        midLine.startAnimation(slide);

        slide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                midLine.clearAnimation();
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                        midLine.getWidth(), midLine.getHeight());
                lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                midLine.setLayoutParams(lp);

                slideToAbove();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

要查找的值实际上是固定的 300。 框是受限制的(以屏幕的百分比表示),因此固定值根本不好。

我刚刚尝试了 trackBox.getHeight(),但不是这样。

刚试过:

        int i;
        i = trackBox.getBottom() - trackBox.getTop();

不工作...

【问题讨论】:

    标签: java android animation layout


    【解决方案1】:

    ConstraintLayout 中终于有一款很棒的条码扫描器了!

    首先你需要在 Res->anim 文件夹中创建 2 个动画文件:

    barcode_to_above_anim.xml

    <?xml version="1.0" encoding="utf-8"?>
    <!-- translating button from bottomt to top -->
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:shareInterpolator="false">
        <translate
            android:fromXDelta="0%" android:toXDelta="0%"
            android:fromYDelta="97%p" android:toYDelta="3%p"
            android:duration="2000"
            />
    </set>
    

    barcode_to_down_anim.xml

    <?xml version="1.0" encoding="utf-8"?>
    <!-- translating button from top to bottom -->
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:shareInterpolator="false">
        <translate
            android:fromXDelta="0%" android:toXDelta="0%"
            android:fromYDelta="3%p" android:toYDelta="97%p"
            android:duration="2000"
            />
    </set>
    

    那么你的活动应该和我的类似:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/linearLayoutBarcodeScanner"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#292929"
        android:screenOrientation="landscape">
    
        <SurfaceView
            android:id="@+id/surfaceViewBarcodeScanner"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintDimensionRatio="16:9"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <RelativeLayout
            android:id="@+id/trackBox"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_centerInParent="true"
            android:background="@drawable/background_border"
            android:paddingStart="2dp"
            android:paddingLeft="2dp"
            android:paddingEnd="2dp"
            android:paddingRight="2dp"
            app:layout_constraintBottom_toTopOf="@+id/guidelineBarcodeScanner04"
            app:layout_constraintEnd_toStartOf="@+id/guidelineBarcodeScanner01"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="@+id/guidelineBarcodeScanner02"
            app:layout_constraintTop_toTopOf="@+id/guidelineBarcodeScanner03"
            app:layout_constraintVertical_bias="0.0">
    
            <RelativeLayout
                android:id="@+id/midLine"
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:background="#FF0000" />
    
        </RelativeLayout>
    
        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guidelineBarcodeScanner01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.8" />
    
        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guidelineBarcodeScanner02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.2" />
    
        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guidelineBarcodeScanner03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.25" />
    
        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guidelineBarcodeScanner04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.75" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    最后是您在 yourActivity.java 中需要的代码:声明

    public class BarcodeScannerActivity extends Activity {
    
        RelativeLayout midLine, trackBox;
        //your other code
    

    引用对象并开始第一个动画:

        private void init(){
            setContentView(R.layout.barcodescanner_activity);
            midLine = findViewById(R.id.midLine);
            trackBox = findViewById(R.id.trackBox);
            slideToDown();
        }
    

    让我们制作动画吧!

        public void slideToAbove() {
            Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.barcode_to_above_anim);
            midLine.startAnimation(slide);
    
            slide.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
    
                }
    
                @Override
                public void onAnimationEnd(Animation animation) {
                    slideToDown();
                }
    
                @Override
                public void onAnimationRepeat(Animation animation) {
    
                }
            });
        }
    
        public void slideToDown() {
    
            Animation slide= AnimationUtils.loadAnimation(getBaseContext(), R.anim.barcode_to_down_anim);
            midLine.startAnimation(slide);
    
            slide.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
    
                }
    
                @Override
                public void onAnimationEnd(Animation animation) {
                    slideToAbove();
                }
    
                @Override
                public void onAnimationRepeat(Animation animation) {
    
                }
            });
    
        }
    

    我应该向你保证,这是一个非常酷的效果! 希望能帮助大家制作出炫酷的条码扫描效果。

    请注意:

        <translate
            android:fromXDelta="0%" android:toXDelta="0%"
            android:fromYDelta="97%p" android:toYDelta="3%p"
            android:duration="2000"
        />
    

    %p 表示引用父视图(或容器或屏幕)的百分比。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-20
      • 2011-08-30
      • 2012-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-15
      相关资源
      最近更新 更多