畫板——顏色板和按鈕

創(chuàng)建一個(gè)類

1.png

在昨天的滑動(dòng)條下面創(chuàng)建畫板和顏色板

1.png
<!--畫板-->
        <swu.lwk.a16draw_point.DrawBloadView
            android:id="@+id/board"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            app:layout_constraintLeft_toRightOf="@id/slider"
            app:layout_constraintRight_toLeftOf="@id/color"/>

        <!--顏色板-->
        <LinearLayout
            android:id="@+id/color"
            android:layout_width="60dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_marginRight="10dp"
            app:layout_constraintRight_toRightOf="parent"
            android:gravity="center">

            <Button
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@color/colorAccent"
                android:onClick="choiceColor"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#000"
                android:onClick="choiceColor"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@color/colorPrimaryDark"
                android:onClick="choiceColor"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@color/colorPrimary"
                android:onClick="choiceColor"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#080888"
                android:onClick="choiceColor"/>

        </LinearLayout>

創(chuàng)建選擇按鈕

1.png
<Button
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="撤銷"
            android:onClick="goBack"/>
        <Button
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="清空"
            android:onClick="clear"/>
        <Button
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="橡皮擦"
            android:onClick="eraser"/>
        <Button
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="上一步"
            android:onClick="lastStep"/>
        <Button
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="保存"
            android:onClick="save"/>

所用到的所有全局變量

1.png

創(chuàng)建init方法用于數(shù)組的初始化和背景的設(shè)置

private void init(){
        //初始化數(shù)組
        graphs=new ArrayList<>();
        orginalGraphs=new ArrayList<>();
        setBackgroundColor(Color.WHITE);
    }

創(chuàng)建畫布用于畫圖和數(shù)組的遍歷

@Override
    protected void onDraw(Canvas canvas) {
        //遍歷數(shù)組
        Iterator<Graph> iterator=graphs.iterator();
        while (iterator.hasNext()){
            //從集合中獲取一個(gè)圖形對象
            Graph line=iterator.next();
            //繪制圖形
            canvas.drawPath(line.path,line.paint);
        }
    }

創(chuàng)建touch事件用于圖形的起點(diǎn)和終點(diǎn)設(shè)置還有其余的信息的設(shè)置

@Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                //創(chuàng)建當(dāng)前這條線對應(yīng)的Paint和Path
                Paint mpaint=new Paint(Paint.ANTI_ALIAS_FLAG);
                mpaint.setColor(lineColor);
                mpaint.setStrokeWidth(lineSize);
                mpaint.setStyle(Paint.Style.STROKE);

                mpath=new Path();
                //設(shè)置起點(diǎn)
                mpath.moveTo(event.getX(),event.getY());
                //保存當(dāng)前這個(gè)圖形的詳細(xì)信息
                Graph temp=new Graph(mpaint,mpath);
                graphs.add(temp);
                orginalGraphs.add(temp);
                break;
            case MotionEvent.ACTION_MOVE:
                //連接從path終點(diǎn)到當(dāng)前觸摸點(diǎn)的線
                mpath.lineTo(event.getX(),event.getY());
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        invalidate();
        return true;
    }

創(chuàng)建私有類管理圖像的畫筆和路徑

private class Graph{
        Paint paint;
        Path path;
        public Graph(Paint paint,Path path){
            this.paint=paint;
            this.path=path;
        }
    }

創(chuàng)建set、get方法

public int getLineSize() {
        return lineSize;
    }

    public void setLineSize(int lineSize) {
        this.lineSize = lineSize;
    }

    public int getLineColor() {
        return lineColor;
    }

    public void setLineColor(int lineColor) {
        this.lineColor = lineColor;
    }

選擇按鈕的具體實(shí)現(xiàn)

//刪除最后一個(gè)圖形  撤銷
    public void removeLast(){
        if (graphs.size()>0){
            graphs.remove(graphs.size()-1);
            invalidate();
        }
    }

    //刪除所有 清空
    public void removeAll(){
        graphs.clear();
        invalidate();
    }

    //還原上一步
    public void lastStep(){
        //判斷緩存中是否有
        if (graphs.size()<orginalGraphs.size()){
            //獲取上一步的索引值
            int index=graphs.size()-1+1;
            //從緩存中獲取index  添加到操作數(shù)組中
            graphs.add(orginalGraphs.get(index));
            //刷新
            invalidate();
        }
    }

獲取畫板

1.png

顏色的選擇

public void choiceColor(View view) {
        //獲取按鈕上面的背景顏色
        ColorDrawable drawable=(ColorDrawable) view.getBackground();
        //獲取顏色
        bloadView.setLineColor(drawable.getColor());
    }

選擇按鈕的獲取

//撤銷
    public void goBack(View view) {
        bloadView.removeLast();
    }

    //清空
    public void clear(View view) {
        bloadView.removeAll();
    }

    //橡皮擦
    public void eraser(View view) {
        //獲取畫板的drawable
        ColorDrawable drawable= (ColorDrawable) bloadView.getBackground();
        //設(shè)置線條的顏色和背景相同
        if (drawable!=null) {
            bloadView.setLineColor(drawable.getColor());
        }
    }

    //保存
    public void save(View view) {

    }

    //還原
    public void lastStep(View view) {
        bloadView.lastStep();
    }
}

最終結(jié)果

1572785505715.gif
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • HTML標(biāo)簽解釋大全 一、HTML標(biāo)記 標(biāo)簽:!DOCTYPE 說明:指定了 HTML 文檔遵循的文檔類型定義(D...
    米塔塔閱讀 3,527評(píng)論 1 41
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,324評(píng)論 0 17
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對...
    cosWriter閱讀 11,641評(píng)論 1 32
  • HTML 5 HTML5概述 因特網(wǎng)上的信息是以網(wǎng)頁的形式展示給用戶的,因此網(wǎng)頁是網(wǎng)絡(luò)信息傳遞的載體。網(wǎng)頁文件是用...
    阿啊阿吖丁閱讀 4,948評(píng)論 0 0
  • 第1篇Objective-C準(zhǔn)備篇 第1章Objective-C學(xué)習(xí)環(huán)境準(zhǔn)備 1.1Objective-C基礎(chǔ) 1...
    YHWXQ簡簡單單的生活閱讀 1,085評(píng)論 2 2

友情鏈接更多精彩內(nèi)容