简体   繁体   English

使用Canvas和Framelayout android绘制圆

[英]Drawing Circles using Canvas and Framelayout android

I am trying to create an app that displays a circle everytime I click a button. 我正在尝试创建一个每次单击按钮都会显示一个圆圈的应用程序。 I have the layout looking great but when i click the button(circle) to display a circle on the screen nothing happens. 我的布局看起来很棒,但是当我单击按钮(圆圈)以在屏幕上显示一个圆圈时,什么也没有发生。 I'm not confident that my draw circle class is being called correctly in my main activity. 我不确定我的主要活动中是否正确调用了我的绘画圈子课程。 Here is my code below. 这是下面的代码。

 package com.example.randomcircles;

 import android.annotation.SuppressLint;
 import android.app.Activity;
 import android.graphics.Canvas;
 import android.graphics.Paint;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.FrameLayout;

public class DisplayRandomCircles extends Activity
{
  DrawCircle c;
  Canvas d;
  @Override
public void onCreate(Bundle b)
{
    super.onCreate(b);
    setContentView(R.layout.activity_display_random_circles);
    Button btn1 = (Button) findViewById(R.id.btn1);
    Button btn2 = (Button) findViewById(R.id.btn2);
    c = new DrawCircle(getApplicationContext());
    d = new Canvas();
    FrameLayout f1 = (FrameLayout) findViewById(R.id.frame);

}
@SuppressLint("WrongCall")
public void doit(View v)
{
    switch (v.getId())
    {
        case (R.id.btn1):
            c.onDraw(d);
            break;

        case (R.id.btn2):
            break;
    }
}
}

Here is my DrawCircle class 这是我的DrawCircle类

package com.example.randomcircles;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class DrawCircle extends View
{
public DrawCircle(Context con)
{
    super(con);
}
@Override
protected void onDraw(Canvas c)
{
    super.onDraw(c);
    Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
    p.setAntiAlias(true);
    p.setStyle(Paint.Style.STROKE);
    p.setStrokeWidth(100);
    p.setColor(Color.RED);
    p.setStyle(Paint.Style.FILL);
    c.drawCircle(75, 75, 100, p);
}
}

And my layout xml 而我的布局xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<FrameLayout
    android:id="@+id/frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight=".75"
    android:orientation="vertical" >


</FrameLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight=".25"
    android:gravity="bottom|center"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="start|bottom"
        android:layout_weight=".50"
        android:onClick="doit"
        android:text="@string/Circle" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".50"
        android:layout_gravity="end|bottom"
        android:onClick="doit"
        android:text="@string/Clear" />
</LinearLayout>

</LinearLayout>

Ok, here are some changes I'd make for this. 好的,这是我要做的一些更改。 I'm not exactly sure what you're trying to do, but this should make things easier. 我不确定您要做什么,但这应该使事情变得容易。

First, change your class "DrawCircle" like this: 首先,如下更改您的类“ DrawCircle”:

 public class DrawCircle extends View
{
  final Paint circlePaint;

  {
      circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
      circlePaint.setAntiAlias(true);
      circlePaint.setStyle(Paint.Style.STROKE);
      circlePaint.setStrokeWidth(100);
      circlePaint.setColor(Color.RED);
      circlePaint.setStyle(Paint.Style.FILL);
  }

  public DrawCircle(Context con)
  {
     super(con);
  }

  public DrawCircle(Context con, AttributeSet set) 
  {
     super(con, set);
  }

  public DrawCircle(Context con, AttributeSet set, int style)
  {
     super(con, set, style);
  }

  @Override
  protected void onDraw(Canvas c)
  {
    super.onDraw(c);
    c.drawCircle(75, 75, 100, circlePaint);
  }
}

This will allow you to reuse the same Paint object with each draw because the onDraw() method can be called hundreds of times and there's no need to slow down your program for this. 这将使您可以在每次绘制时重复使用相同的Paint对象,因为onDraw()方法可以被调用数百次,因此无需为此降低程序速度。

Second, adding the other constructors allows you to add the View to your FrameLayouts by xml. 其次,添加其他构造函数使您可以通过xml将视图添加到FrameLayouts中。

<FrameLayout
    android:id="@+id/frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

  <com.example.randomCircles.DrawCircle
     android:id="@+id/circleFrame"
     android:layout_width="match_parent"
     android:layout_height="match_parent" />

</FrameLayout>

Next, to fill your screen, you need to loop in your onDraw method. 接下来,要填充屏幕,您需要循环使用onDraw方法。 Think about drawing on the canvas as a stamp. 考虑在画布上作图章。 Every time you draw, you stamp your image at the location you specify on top of the previous draw. 每次绘制时,都在上一个绘制上方指定的位置标记图像。

So 所以

protected void onDraw(Canvas c) 
{
  super.onDraw(c);
  for(int i = 0; i < 10; i++)
  {
     c.drawCircle(i*100, 75, 100, circlePaint);
  }
} 

This should draw 10 circles of radius 100 pixels across the top of your screen. 这应该在屏幕顶部绘制10个半径为100像素的半径的圆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM