亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android三種方式生成矢量圖之VectorDrawable類使用詳解

 更新時(shí)間:2023年02月16日 11:03:49   作者:weixin_43912367  
這篇文章主要介紹了Android三種方式生成矢量圖的VectorDrawable類,2014年6月26日的I/O?2014開發(fā)者大會(huì)上谷歌正式推出了Android?L,它帶來(lái)了全新的設(shè)計(jì)語(yǔ)言Material?Design,新的API也提供了這個(gè)類VectorDrawable

生成矢量圖VectorDrawable的三種方式

  • 第一種:

選中drawable文件夾,右鍵New --> Vector Asset --> 選中Clip Art ,在這里面可以選擇一些矢量圖 ,點(diǎn)擊Next,然后 Finish即可。

  • 第二種:(前提:自己有一張svg或psd的圖片)

選中drawable文件夾,右鍵New --> Vector Asset --> 選中Local file ,在這里面選擇自己svg路徑 ,點(diǎn)擊Next,然后 Finish即可。

  • 第三種:

自己編寫代碼

靜態(tài)VectorDrawable的使用

配置引用和參數(shù)

gradle文件種按下面配置:

android {
    defaultConfig {
        ...
        vectorDrawables.useSupportLibrary = true
    }

  ...

dependencies {
   ...

    implementation 'com.android.support:appcompat-v7:23.4.0'
}

注意:support:appcompat 必須在23.2以上的引用

在控件中使用

ImageView\ImageButtom

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/ic_aaa"
        />

既然這樣可以顯示,那我們?nèi)绶ㄅ谥?,設(shè)置在Button上面會(huì)怎么樣呢?

<Button
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:srcCompat="@drawable/ic_aaa"
        />

結(jié)果超出我們的想象,并不能達(dá)到我們預(yù)期的效果。原因是:Button是一個(gè)有不同狀態(tài)的控件,為了程序的嚴(yán)謹(jǐn)性,所以無(wú)效。

那我們對(duì)他的每一個(gè)狀態(tài)進(jìn)行設(shè)置,可不可行呢?

在drawable文件夾下新建xml文件,名為demo

demo代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_aaa" android:state_pressed="true"/>
    <item android:drawable="@drawable/ic_bubble_chart_black_24dp"/>
</selector>

然后在Button里面引用:

<Button
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/demo"
        />

然后運(yùn)行:

原始狀態(tài):

按下狀態(tài):

ok,這就大功告成了。

這里要說(shuō)明的一點(diǎn),我這邊呢,使用的Android Studio是目前最新的3.6.3,如果AndroidStudio版本較低的話,可能會(huì)出現(xiàn)閃退的現(xiàn)象。這是VectorDrawable的一個(gè)坑點(diǎn)。

解決方法:在相對(duì)應(yīng)的Activity中加入以下代碼即可

static{
        AppCompatDelegate
            .setCompatVectorFromResourcesEnabled(true);
    }    

此篇結(jié)束,下篇文章將繼續(xù)講解Android中使用動(dòng)態(tài)的VectorDrawable

到此這篇關(guān)于Android三種方式生成矢量圖之VectorDrawable類使用詳解的文章就介紹到這了,更多相關(guān)Android VectorDrawable內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論