iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 29
1
Mobile Development

Android 開發經驗三十天系列 第 29

[Android 開發經驗三十天]D29一小畫家小問題跟改善方法

職涯在走,鐵人賽文章一定要有。

小畫家小問題跟改善方法

tags: 鐵人賽

嗨,大家安安,今天來說說小畫家小問題跟改善方法

Q1:橡皮擦不能擦圖層只能改變顏色?

A1:橡皮擦擦圖層要加上

setWillNotDraw(false);
setLayerType(LAYER_TYPE_HARDWARE, null);

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

可以看一下setXfermode的圖,我們看到clear那張他就是將整個圖層擦去,所以我們小畫家就是用這個方法當作橡皮擦

用兩個圖層,下層是放圖片,上層是用自定義View小畫家

這樣就可以讓我們的自定義View小畫家可以當作圖片編輯器的一種

Q:如何上下分開?
A:用FramLayout可以用上下圖層
如圖所示,這樣底層是彩虹小馬上層是自定義View小畫家

Q:databinding使用有問題?
A:之前用不同裝置大小layout都綁databinding的時候有遇過問題
讀不到其中一個大小的layout,可以使用bulid -> clear project
再重build,因為有可能是沒重build吃不到的問題
code:

<FrameLayout 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:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


<!--app:srcCompat="@drawable/ic_unicorn"-->
        <ImageView
            app:srcCompat="@drawable/ic_unicorn"
            android:id="@+id/horse"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintTop_toTopOf="parent"
             />
        <com.example.ktforfilemanager.paintview.PPView
            android:id="@+id/paintView"
            android:layout_width="0dp"
            android:layout_height="692dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/widthprogress" />
        <LinearLayout
            android:id="@+id/linearLayout3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <Button
                android:id="@+id/eraser"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="@drawable/eraser"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <Button
                android:id="@+id/go"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="@drawable/ic_paintbrush"
                app:layout_constraintStart_toEndOf="@+id/eraser"
                app:layout_constraintTop_toTopOf="parent" />

            <Button
                android:id="@+id/back"
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_undo_black_24dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toEndOf="@+id/go"
                app:layout_constraintTop_toTopOf="parent" />
            <Button
                android:id="@+id/next"
                android:background="@drawable/ic_redo_black_24dp"
                android:layout_width="40dp"
                android:layout_height="wrap_content"/>

            <Button
                android:id="@+id/save"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="@drawable/ic_save_black_24dp" />

            <Button
                android:id="@+id/turn"
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_arrow_forward_black_24dp"/>
            <Button
                android:id="@+id/cloud"
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_cloud_black_24dp"/>
            <Button
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                android:id="@+id/water"/>
        </LinearLayout>


        <SeekBar
            android:id="@+id/widthprogress"
            android:layout_width="0dp"
            android:layout_height="25dp"
            android:visibility="gone"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/linearLayout3"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout3" />

        <SeekBar
            android:visibility="gone"
            android:id="@+id/transparenctbar"
            android:layout_width="0dp"
            android:layout_height="25dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/widthprogress" />
        <SeekBar
            android:visibility="gone"
            android:id="@+id/eraserprogress"
            android:layout_width="0dp"
            android:layout_height="25dp"

            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/linearLayout3"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout3" />
        <!--  android:visibility="gone"-->

        <LinearLayout
            android:id="@+id/chColorLayout"
            android:visibility="gone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/transparenctbar">

            <Button
                android:id="@+id/red"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_margin="10dp"
                android:background="@color/colorRed" />

            <Button
                android:id="@+id/yellow"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_margin="10dp"
                android:background="@color/colorYy" />
            <Button
                android:id="@+id/black"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_margin="10dp"
                android:background="#000000" />

            <Button
                android:id="@+id/blue"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_margin="10dp"
                android:background="#3F51B5" />
            <Button
                android:id="@+id/orange"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_margin="10dp"
                android:background="#E91E63" />
            <Button
                android:id="@+id/green"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_margin="10dp"
                android:background="#4CAF50" />
        </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>

今天就差不多這樣啦,其他明天見


上一篇
[Android 開發經驗三十天]D28一 Fragment+ViewPager+Tablayout
下一篇
[Android 開發經驗三十天]#D30 完賽心得
系列文
Android 開發經驗三十天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言