Android簡單圖形示例含代碼

Android簡單圖形示例

android.graphics.Canvas 可以用來在Android中繪制圖形。愛掏網 - it200.com它提供了繪制橢圓、矩形、圖片、文本、線等的方法。愛掏網 - it200.com

android.graphics.Paint 類與畫布一起使用以繪制對象。愛掏網 - it200.com它保存了顏色和樣式的信息。愛掏網 - it200.com

在這個示例中,我們將在Android中顯示2D圖形。愛掏網 - it200.com

activity_main.xml

文件:activity_main.xml

<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

Activity類

文件:MainActivity.java

package com.example.simplegraphics;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class MainActivity extends Activity {

    DemoView demoview;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        demoview = new DemoView(this);
        setContentView(demoview);
    }

    private class DemoView extends View{
        public DemoView(Context context){
            super(context);
        }

        @Override protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            // custom drawing code here
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);

            // make the entire canvas white
            paint.setColor(Color.WHITE);
            canvas.drawPaint(paint);

            // draw blue circle with anti aliasing turned off
            paint.setAntiAlias(false);
            paint.setColor(Color.BLUE);
            canvas.drawCircle(20, 20, 15, paint);

            // draw green circle with anti aliasing turned on
            paint.setAntiAlias(true);
            paint.setColor(Color.GREEN);
            canvas.drawCircle(60, 20, 15, paint);

            // draw red rectangle with anti aliasing turned off
            paint.setAntiAlias(false);
            paint.setColor(Color.RED);
            canvas.drawRect(100, 5, 200, 30, paint);

            // draw the rotated text
            canvas.rotate(-45);

            paint.setStyle(Paint.Style.FILL);
            canvas.drawText("Graphics Rotation", 40, 180, paint);

            //undo the rotate
            canvas.restore();
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

輸出:

聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
發表評論
更多 網友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 亚洲成人网在线观看| 91探花视频在线观看| 亚洲毛片基地4455ww| 国产精品久久福利网站| 最新仑乱免费视频| 色欲综合久久中文字幕网| a毛片全部免费播放| 九九全国免费视频| 又硬又粗进去好爽免费| 国产精品青青青高清在线观看| 日韩欧美成人免费中文字幕| 精品欧美日韩一区二区| 97久久香蕉国产线看观看| 久久免费看少妇高潮V片特黄| 伊人色综合久久大香| 国产成人无码a区在线观看视频免费| 成a人片亚洲日本久久| 欧美一级黄色片视频| 精品乱码久久久久久中文字幕| 91视频免费观看| 三级4级做a爰60分钟| 二区三区在线观看| 亚洲精品无码mv在线观看 | 亚洲视频国产精品| 国产亚洲欧美另类专区| 国语自产少妇精品视频| 无人高清视频免费观看在线动漫 | 月夜直播手机免费视频高清| 精品无人区乱码1区2区| 黑执事第二季免费观看| 99热在线观看| 一本色道无码道在线观看| 亚洲AV无码之日韩精品| 亚洲欧美日韩精品久久亚洲区 | 亚洲av日韩精品久久久久久久| 亚洲黄色在线看| 内射一区二区精品视频在线观看| 国产成人精品无码一区二区老年人| 国产色婷婷精品综合在线| 天堂网在线.www天堂在线资源| 巨大黑人极品hdvideo|