在values里边建一个attrs.xml布局:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyTextView2">
<attr name="myText" format="string"/>
<attr name="myTextColor" format="color"/>
<attr name="myTextSize" format="dimension"/>
</declare-styleable>
</resources>
在主布局activity_main里边写代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.bwie.lianxi1.ViewClass
android:id="@+id/Bull"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
在第二个布局页面写代码如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HelloActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="欢迎来到本页面"
android:gravity="center"/>
</android.support.constraint.ConstraintLayout>
在主方法里建一个ViewClass类,代码如下:
package com.bwie.lianxi1;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
* @Auther: 祁壮壮
* @Date: 2019/3/22
* @Description:
*/
public class ViewClass extends View {
public ViewClass(Context context) {
super(context);
}
public ViewClass(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ViewClass(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public ViewClass(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
//测量
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
//绘制
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//圆
Paint pal = new Paint();
pal.setColor(Color.BLUE);
pal.setAntiAlias(true);
canvas.drawCircle(50,50,50,pal);
}
//定位
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
//监听
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
}
在mainActivity里边代码如下:
package com.bwie.lianxi1;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
ViewClass MyView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyView = findViewById(R.id.Bull);
ObjectAnimator move = ObjectAnimator.ofFloat(MyView, "translationY", 0, 1100f);
ObjectAnimator scale2 = ObjectAnimator.ofFloat(MyView, "translationX",0,600f);
AnimatorSet animset = new AnimatorSet();
animset.play(move).with(scale2);
animset.setDuration(5000);
animset.start();
//动画监听
animset.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
Intent intent = new Intent(MainActivity.this, HelloActivity.class);
startActivity(intent);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
}
第二个主方法代码:
package com.bwie.lianxi1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class HelloActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello);
}
}