欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

垂直与抛物线

程序员文章站 2024-03-25 12:33:52
...

绘制

public class CustomView extends View {
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomView(Context context) {
    super(context);
}
//测量

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
//重写OnDraw绘制基本图形

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //先创建画笔
    Paint paint = new Paint();
    //设置画笔颜色
    paint.setColor(Color.BLUE);
    // 设置抗锯齿
    paint.setAntiAlias(true);
    // 三种样式
    paint.setStyle(Paint.Style.FILL_AND_STROKE);
    //设置边的宽度
    paint.setStrokeWidth(5);

    // 阴影
    paint.setShadowLayer(10, 0, 0, Color.CYAN);

    //绘制圆
    canvas.drawCircle(30, 30, 15, paint);
}

}

布局

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout… 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/id_container"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".activity.MainActivity">

<com.bwie.yuan.activity.CustomView
    android:id="@+id/ball"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal">

    <Button
        android:id="@+id/button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="垂直"
        android:textSize="20sp" />

    <Button
        android:id="@+id/button02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="抛物线"
        android:textSize="20sp" />

    <Button
        android:id="@+id/button03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转"
        android:textSize="20sp" />
</LinearLayout>

</RelativeLayout…>

主页

import com.bwie.yuan.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private CustomView ball;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //找控件
    ball = findViewById(R.id.ball);

    Button button1 = findViewById(R.id.button01);
    Button button2 = findViewById(R.id.button02);


    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
    findViewById(R.id.button03).setOnClickListener(this);

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button01:

            ValueAnimator va1 = new ValueAnimator();
            va1.setDuration(3000);
            va1.setObjectValues(new PointF(0, 0));
            va1.setInterpolator(new LinearInterpolator());
            va1.setEvaluator(new TypeEvaluator<PointF>() {
                @Override
                public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
                    PointF point = new PointF();
                    point.x = 0;
                    point.y = 0.5f * 200 * (fraction * 3) * (fraction * 3);

                    return point;
                }
            });
            va1.start();
            va1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    PointF point = (PointF) animation.getAnimatedValue();
                    ball.setX(point.x);
                    ball.setY(point.y);
                }
            });
            break;
        case R.id.button02:
            ValueAnimator va = new ValueAnimator();
            va.setDuration(3000);
            va.setObjectValues(new PointF(0, 0));
            va.setInterpolator(new LinearInterpolator());
            va.setEvaluator(new TypeEvaluator<PointF>() {
                @Override
                public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
                    PointF point = new PointF();
                    point.x = 200 * fraction * 3;
                    point.y = 0.5f * 200 * (fraction * 3) * (fraction * 3);

                    return point;
                }
            });
            va.start();
            va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    PointF point = (PointF) animation.getAnimatedValue();
                    ball.setX(point.x);
                    ball.setY(point.y);
                }
            });

            break;
        case R.id.button03:
            startActivity(new Intent(MainActivity.this, ShowActivity.class));
            finish();
            break;
    }
}

}

上一篇: Html,Css

下一篇: api接口—淘宝搜索