簡體   English   中英

在Android Studio中添加延遲計時器

[英]Add a delay timer in Android Studio

FaceView.java

        package com.example.richelle.neeuro;
        public class FaceView extends View {
            private float radius;
            NormalFace normalFace;
            HappyFace happyFace;

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

                // get radius value
                TypedArray a = context.getTheme().obtainStyledAttributes(
                        attrs,
                        R.styleable.FaceView,
                        0, 0
                );

                try {
                    radius = a.getDimension(R.styleable.FaceView_radius, 20.0f);
                } finally {
                    a.recycle();
                }

                // initiate HappyFace class
                normalFace = new NormalFace(radius);
                happyFace = new HappyFace(radius);
            }

            Handler setDelay;
            Runnable startDelay;

            @Override
            protected void onDraw(final Canvas canvas) {
                super.onDraw(canvas);
                normalFace.draw(canvas);
                //delay timer
                setDelay = new Handler();
                startDelay = new Runnable() {
                    @Override
                    public void run() {
                        happyFace.draw(canvas);
                    }
                };
                setDelay.postDelayed(startDelay,5000);
            }

            @Override
            protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);

                int desiredWidth = (int) radius*2+(int) Math.ceil((radius/1.70));
                int desiredHeight = (int) radius*2+(int)Math.ceil((radius/1.70));

                int widthMode = MeasureSpec.getMode(widthMeasureSpec);
                int widthSize = MeasureSpec.getSize(widthMeasureSpec);
                int heightMode = MeasureSpec.getMode(heightMeasureSpec);
                int heightSize = MeasureSpec.getSize(heightMeasureSpec);

                int width;
                int height;

                //Measure Width
                if (widthMode == MeasureSpec.EXACTLY) {
                    //Must be this size
                    width = widthSize;
                } else if (widthMode == MeasureSpec.AT_MOST) {
                    //Can't be bigger than...
                    width = Math.min(desiredWidth, widthSize);
                    Log.d("Width AT_MOST", "width: "+width);
                } else {
                    //Be whatever you want
                    width = desiredWidth;
                    Log.d("Width ELSE", "width: "+width);

                }

                //Measure Height
                if (heightMode == MeasureSpec.EXACTLY) {
                    //Must be this size
                    height = heightSize;
                } else if (heightMode == MeasureSpec.AT_MOST) {
                    //Can't be bigger than...
                    height = Math.min(desiredHeight, heightSize);
                } else {
                    //Be whatever you want
                    height = desiredHeight;
                }

                //MUST CALL THIS
                setMeasuredDimension(width, height);
            }
            public float getRadius() {
                return radius;
            }

            public void setRadius(float radius) {
                this.radius = radius;
            }

        }

NormalFace.java

        public class NormalFace {

            //Paint object
            Paint facePaint;
            Paint mePaint;

            float radius;
            float adjust;

            float mouthLeftX, mouthRightX, mouthTopY, mouthBottomY;
            RectF mouthRectF;
            Path mouthPath;

            float eyeLeftX, eyeRightx, eyeTopY, eyeBottomY;
            RectF eyeLeftRectF, eyeRightRectF;

            public NormalFace(float radius){
                this.radius= radius;

                facePaint = new Paint();
                facePaint.setColor(0xfffed325); // face color - yellow
                facePaint.setDither(true);
                facePaint.setStrokeJoin(Paint.Join.ROUND);
                facePaint.setStrokeCap(Paint.Cap.ROUND);
                facePaint.setPathEffect(new CornerPathEffect(10) );
                facePaint.setAntiAlias(true);
                facePaint.setShadowLayer(4, 2, 2, 0x80000000);

                mePaint = new Paint();
                mePaint.setColor(0xff2a2a2a); //black
                mePaint.setDither(true);
                mePaint.setStyle(Paint.Style.STROKE);
                mePaint.setStrokeJoin(Paint.Join.ROUND);
                mePaint.setStrokeCap(Paint.Cap.ROUND);
                mePaint.setPathEffect(new CornerPathEffect(10) );
                mePaint.setAntiAlias(true);
                mePaint.setStrokeWidth(radius / 14.0f);

                adjust = radius / 3.2f;

                // Left Eye
                eyeLeftX = radius-(radius*0.43f);
                eyeRightx = eyeLeftX + (radius*0.3f);
                eyeTopY = radius-(radius*0.5f);
                eyeBottomY = eyeTopY + (radius*0.4f);

                eyeLeftRectF = new RectF(eyeLeftX+adjust,eyeTopY+adjust,eyeRightx+adjust,eyeBottomY+adjust);

                // Right Eye
                eyeLeftX = eyeRightx + (radius*0.3f);
                eyeRightx = eyeLeftX + (radius*0.3f);

                eyeRightRectF = new RectF(eyeLeftX+adjust,eyeTopY+adjust,eyeRightx+adjust,eyeBottomY+adjust);

                // Mouth
                mouthLeftX = radius-(radius/2.0f);
                mouthRightX = mouthLeftX+ radius;
        //        mouthTopY = 125 - (125*0.01f);
        //        mouthBottomY = mouthTopY + (125*0.01f);
                mouthTopY = radius - (radius*(-0.2f));
                mouthBottomY = mouthTopY + (radius*0.01f);

                mouthRectF = new RectF(mouthLeftX+adjust,mouthTopY+adjust,mouthRightX+adjust,mouthBottomY+adjust);
                //mouthRectF = new RectF(mouthLeftX+adjust,mouthTopY+70,mouthRightX+adjust,mouthBottomY+20); //a line

                mouthPath = new Path();

                mouthPath.arcTo(mouthRectF, 30, 120, true);
        //        mouthPath.arcTo(mouthRectF, 15, 135, true);
            }

            public void draw(Canvas canvas) {

                // 1. draw face
                canvas.drawCircle(radius+adjust, radius+adjust, radius, facePaint);

                // 2. draw mouth
                mePaint.setStyle(Paint.Style.STROKE);

                canvas.drawPath(mouthPath, mePaint);
                //canvas.drawLine(90, 155, 176, 155, mePaint);

                // 3. draw eyes
                mePaint.setStyle(Paint.Style.FILL);
                canvas.drawArc(eyeLeftRectF, 0, 360, true, mePaint);
                canvas.drawArc(eyeRightRectF, 0, 360, true, mePaint);

            }
        }

HappyFace.java

        public class HappyFace {

            //Paint object
            Paint facePaint;
            Paint mePaint;

            float radius;
            float adjust;

            float mouthLeftX, mouthRightX, mouthTopY, mouthBottomY;
            RectF mouthRectF;
            Path mouthPath;

            float eyeLeftX, eyeRightx, eyeTopY, eyeBottomY;
            RectF eyeLeftRectF, eyeRightRectF;

            public HappyFace(float radius){
                this.radius= radius;

                facePaint = new Paint();
                facePaint.setColor(0xfffed325); // face color - yellow
                facePaint.setDither(true);
                facePaint.setStrokeJoin(Paint.Join.ROUND);
                facePaint.setStrokeCap(Paint.Cap.ROUND);
                facePaint.setPathEffect(new CornerPathEffect(10) );
                facePaint.setAntiAlias(true);
                facePaint.setShadowLayer(4, 2, 2, 0x80000000);

                mePaint = new Paint();
                mePaint.setColor(0xff2a2a2a); //black
                mePaint.setDither(true);
                mePaint.setStyle(Paint.Style.STROKE);
                mePaint.setStrokeJoin(Paint.Join.ROUND);
                mePaint.setStrokeCap(Paint.Cap.ROUND);
                mePaint.setPathEffect(new CornerPathEffect(10) );
                mePaint.setAntiAlias(true);
                mePaint.setStrokeWidth(radius / 14.0f);

                adjust = radius / 3.2f;

                // Left Eye
                eyeLeftX = radius-(radius*0.43f);
                eyeRightx = eyeLeftX + (radius*0.3f);
                eyeTopY = radius-(radius*0.5f);
                eyeBottomY = eyeTopY + (radius*0.4f);

                eyeLeftRectF = new RectF(eyeLeftX+adjust,eyeTopY+adjust,eyeRightx+adjust,eyeBottomY+adjust);

                // Right Eye
                eyeLeftX = eyeRightx + (radius*0.3f);
                eyeRightx = eyeLeftX + (radius*0.3f);

                eyeRightRectF = new RectF(eyeLeftX+adjust,eyeTopY+adjust,eyeRightx+adjust,eyeBottomY+adjust);


                // Smiley Mouth
                mouthLeftX = radius-(radius/2.0f);
                mouthRightX = mouthLeftX+ radius;
                mouthTopY = radius - (radius*0.2f);
                mouthBottomY = mouthTopY + (radius*0.5f);

                mouthRectF = new RectF(mouthLeftX+adjust,mouthTopY+adjust,mouthRightX+adjust,mouthBottomY+adjust);
                mouthPath = new Path();

                mouthPath.arcTo(mouthRectF, 30, 120, true);
            }

            public void draw(Canvas canvas) {

                // 1. draw face
                canvas.drawCircle(radius+adjust, radius+adjust, radius, facePaint);

                // 2. draw mouth
                mePaint.setStyle(Paint.Style.STROKE);

                canvas.drawPath(mouthPath, mePaint);

                // 3. draw eyes
                mePaint.setStyle(Paint.Style.FILL);
                canvas.drawArc(eyeLeftRectF, 0, 360, true, mePaint);
                canvas.drawArc(eyeRightRectF, 0, 360, true, mePaint);

            }
        }

FaceView.java用於在畫布中顯示不同的面部表情。 NormalFace.java和HappyFace.java是不同面部表情的UI。 我想在FaceView.java中添加一個延遲計時器,以便在計時器完成遞減計數后,可以將正常人臉的顯示更改為滿意的表情。

您可以使用Handler類這android.Handler有一個方法postDelayed()您可以使用此方法的延遲,從讀到處理器這里

 Runnable happy = new Runnable() {
public void run() {
    happyFace(); //suppose this is the method for happy face
}
};

然后在5秒鍾后像這樣調用此方法

 handler.postDelayed(happy,5000);

它將發布您的代碼,以在5秒鍾后運行

您可以使用Handler延遲下一個任務

import android.os.Handler;
    Handler handler=new Handler();
                Runnable r=new Runnable() {
                    public void run() {
                        // Your next task
                    }
                };
                handler.postDelayed(r, 30000);

30000是延遲值(以毫秒為單位),表示為30秒

使用此:ms是延遲(以毫秒為單位)

try {
        Thread.sleep(ms);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM