【问题标题】:Android make canvas cover half screenAndroid 使画布覆盖半屏
【发布时间】:2017-09-18 13:06:56
【问题描述】:

我正在尝试创建一个用于在画布中绘制一些简单线条的应用程序。我希望画布占据屏幕的顶部 50%,将屏幕底部的 50% 用于按钮等等。我目前正在使用setContentView(canvas);,这使得画布占据了 100% 的屏幕。

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以创建一个扩展 View 的类,并在该视图的 onDraw() 方法上在该视图内进行绘图。使用您选择的任何宽度或高度将该视图添加到您的布局中。

    【讨论】:

    • 如何将其添加到我选择的宽度和高度的布局中?
    • 假设您在 com.example.views java 文件夹中创建了一个类 MyView extends View。您将其添加到 xml 作为添加另一个视图(TextView、imageView 等)或在运行时实例化它。您可以搜索在运行时或 xml 上添加自定义视图,您将获得大量示例。调用 myView.invalidate() 会调用该视图的 onDraw(Canvas canvas) ,您可以按照与画布相同的方式绘制任何内容。
    【解决方案2】:

    代替setContentView,使用您的布局XML创建一个视图来保存画布,如Error referencing an inner class View in layout/main.xml所示。

    之后,就是在android:layout_height 50% of the screen size中使用一种技术比如:

    <LinearLayout ...>
    <view
        class="com.example.foo.FooActivity$Drawer"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
    />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
    >
    </LinearLayout>
    

    您还可以使用 android:layout_weight="0.7""0.3" 来分别将画布/按钮容器设置为屏幕的 70% 和 30%。

    这是一个最小的完整示例:

    布局:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:keepScreenOn="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:background="#f00"
    >
        <view
            class="com.example.foo.FooActivity$Drawer"
            android:background="#00f"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
        />
        <LinearLayout
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
        >
            <Button
                android:id="@+id/start"
                android:text="@string/start"
                android:textSize="20sp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:backgroundTint="#f0f"
                android:padding="10dp"
                android:layout_margin="20dp"
            />
            <Button
                android:id="@+id/stop"
                android:text="@string/stop"
                android:textSize="20sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:backgroundTint="#f0f"
                android:padding="10dp"
                android:layout_margin="20dp"
            />
        </LinearLayout>
    </LinearLayout>
    

    活动:

    package com.example.foo;
    
    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.os.Bundle;
    import android.util.AttributeSet;
    import android.view.View;
    
    public class FooActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_foo);
        }
    
        public static class Drawer extends View {
            private static Paint paint;
            private static int viewWidth;
            private static int viewHeight;
    
            public Drawer(Context con, AttributeSet attr) {
                super(con, attr);
                paint = new Paint(Paint.ANTI_ALIAS_FLAG);
                paint.setStyle(Paint.Style.FILL);
            }
    
            // https://stackoverflow.com/a/9718512/6243352
            @Override
            protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld) {
                super.onSizeChanged(xNew, yNew, xOld, yOld);
                viewWidth = xNew;
                viewHeight = yNew;
            }
    
            @Override
            protected void onDraw(final Canvas c) {
                super.onDraw(c);
                paint.setARGB(255, 0, 0, 0);
                int cx = viewWidth / 2;
                int cy = viewHeight / 2;
                c.drawCircle(cx, cy,viewWidth / 4, paint);
            }
        }
    }
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-03
      • 2011-02-24
      • 1970-01-01
      • 1970-01-01
      • 2019-07-12
      • 2016-01-27
      • 2016-06-22
      • 2010-12-12
      相关资源
      最近更新 更多