如何在 xml 布局上使用 onDraw()?无效不刷新onDraw()函数
How to use onDraw() over xml layout? Invalidate not refreshing the onDraw() function
我一直在尝试在地图上绘制圆圈作为玩家位置,但我想使用 XML 来布置布局周围的其他内容,例如按钮,将地图作为背景。但是,我似乎无法让这两件事一起工作。如果我使用自定义视图 class 作为 setContentView,它们可以很好地分开工作并且可以很好地绘制内容。但是,如果我在 xml 中使用 customview,onDraw() 函数只会在开始时执行一次并且不能被 invalidate()'d。
这是布局XML:
<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=".TrackMap">
<com.b143lul.android.logreg.CircleView android:id="@+id/CircleView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/resized_track_2"
></com.b143lul.android.logreg.CircleView>
而我在自定义视图中的代码是:
public class CircleView extends View {
SharedPreferences sharedPreferences;
JSONObject groupScores;
Paint paint1;
String localUsername = "";
public CircleView(Context context) {
super(context);
init(context);
}
public CircleView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public void init(Context context) {
paint1 = new Paint();
paint1.setColor(Color.BLUE);
this.setWillNotDraw(false);
}
protected void onDraw(Canvas canvas) {
// Bunch of circle drawing.
}
public void update(JSONObject allGroupScores, String username) {
groupScores = allGroupScores;
localUsername = username;
invalidate();
// Invalidate being used to refresh the draw function
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
至于我如何实现自定义视图,我从 onCreate() 开始:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
circleView = new CircleView(this);
//setContentView(circleView);
setContentView(R.layout.activity_track_map);
然后在每 5 秒获取分数的 StringRequest 之后调用自定义视图 class 的更新函数(一切正常):
try {
groupScores = new JSONObject(response);
circleView.update(groupScores, username);
所以,我只是想知道如何同时使用两者,以便绘图超过 XML。可悲的是,我似乎无法在网上找到任何东西,如果我真的没有做这件事,我很抱歉!提前致谢! :)
仅需提及:正在访问 update() 函数,但 invalidate()
未使 onDraw() 刷新。如果该视图用作 setContentView(),则这是 运行。
原来,
circleView = new CircleView(this);
加载 XML 之外的 class,这是问题的一部分。但是,当我尝试通过 findViewById() 加载视图时,出现了 NullPointerException。因此,在尝试通过视图获取 class 之前,需要调用 setContentView({Your layout XML}) 。又名解决方案是:
setContentView(R.layout.activity_track_map);
circleView = (CircleView)findViewById(R.id.CircleView);
我一直在尝试在地图上绘制圆圈作为玩家位置,但我想使用 XML 来布置布局周围的其他内容,例如按钮,将地图作为背景。但是,我似乎无法让这两件事一起工作。如果我使用自定义视图 class 作为 setContentView,它们可以很好地分开工作并且可以很好地绘制内容。但是,如果我在 xml 中使用 customview,onDraw() 函数只会在开始时执行一次并且不能被 invalidate()'d。
这是布局XML:
<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=".TrackMap">
<com.b143lul.android.logreg.CircleView android:id="@+id/CircleView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/resized_track_2"
></com.b143lul.android.logreg.CircleView>
而我在自定义视图中的代码是:
public class CircleView extends View {
SharedPreferences sharedPreferences;
JSONObject groupScores;
Paint paint1;
String localUsername = "";
public CircleView(Context context) {
super(context);
init(context);
}
public CircleView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public void init(Context context) {
paint1 = new Paint();
paint1.setColor(Color.BLUE);
this.setWillNotDraw(false);
}
protected void onDraw(Canvas canvas) {
// Bunch of circle drawing.
}
public void update(JSONObject allGroupScores, String username) {
groupScores = allGroupScores;
localUsername = username;
invalidate();
// Invalidate being used to refresh the draw function
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
至于我如何实现自定义视图,我从 onCreate() 开始:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
circleView = new CircleView(this);
//setContentView(circleView);
setContentView(R.layout.activity_track_map);
然后在每 5 秒获取分数的 StringRequest 之后调用自定义视图 class 的更新函数(一切正常):
try {
groupScores = new JSONObject(response);
circleView.update(groupScores, username);
所以,我只是想知道如何同时使用两者,以便绘图超过 XML。可悲的是,我似乎无法在网上找到任何东西,如果我真的没有做这件事,我很抱歉!提前致谢! :)
仅需提及:正在访问 update() 函数,但 invalidate()
未使 onDraw() 刷新。如果该视图用作 setContentView(),则这是 运行。
原来,
circleView = new CircleView(this);
加载 XML 之外的 class,这是问题的一部分。但是,当我尝试通过 findViewById() 加载视图时,出现了 NullPointerException。因此,在尝试通过视图获取 class 之前,需要调用 setContentView({Your layout XML}) 。又名解决方案是:
setContentView(R.layout.activity_track_map);
circleView = (CircleView)findViewById(R.id.CircleView);