javascript

java

python

c#

node.js

reactjs

android

php

c++

ruby-on-rails

html

jquery

git

css

ios

sql

.net

c

<img src="//i.stack.imgur.com/bk9VA.png" alt="" class="sponsor-tag-img" width="18" height="16"/>android

string

android - 从矢量绘图获取位图

在我的应用程序中,我必须为通知设置一个大图标。LargeIcon必须是一个Bitmap,我的drawables是矢量图像(Android中的新功能,请看这个链接)问题是当我尝试解码作为矢量图像的资源时,我得到一个null返回。

以下是代码示例:

if (BitmapFactory.decodeResource(arg0.getResources(), R.drawable.vector_menu_objectifs) == null)
        Log.d("ISNULL", "NULL");
    else
        Log.d("ISNULL", "NOT NULL");

在此示例中,当我用&#34; normal&#34;替换R.drawable.vector_menu_objectifs时。 image,例如png,结果不为null(我得到正确的位图)有什么我想念的吗?

trans by 2019-08-13T22:26:39Z

java - 是否可以使用android:DrawableRight在Buttons和TextViews中使用VectorDrawable?

当我在textview或imageview中使用VectorDrawable资源时,使用“android:DrawableRight”/“android:DrawableEnd”/“android:DrawableStart”/“android:DrawableLeft”时会出现运行时崩溃。

该应用程序将编译正常,没有任何警告。

我在用

  • Gradle 1.5
  • 支持库23.2('com.android.support:appcompat-v7:23.2.0')

我发现的是,我可以通过编程方式在Java中分配SVG,而不会像这样崩溃。

TextView tv = (TextView) findViewById(R.id.textView);
tv.setCompoundDrawablesWithIntrinsicBounds(null,null, getResources().getDrawable(R.drawable.ic_accessible_white_36px),null);

(我怀疑这是23.2的支持库错误。)

但是可以将drawableRight等用于SVG资产吗?

这是我的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="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="au.com.angryitguy.testsvg.MainActivity">


<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawableRight="@drawable/ic_accessible_white_36px"
    android:background="@color/colorPrimary"
    android:textColor="#FFFFFF"
    android:textSize="22sp"
    android:text="Hello World!"/>
</RelativeLayout>

这是我的活动

package au.com.angryitguy.testsvg;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        }
    }

以下是Google材料设计网站中未经修改的VectorDrawable资产。

<vector android:height="24dp" android:viewportHeight="24.0"
    android:viewportWidth="24.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#FFFFFF" android:pathData="M12,4m-2,0a2,2 0,1 1,4 0a2,2 0,1 1,-4 0"/>
    <path android:fillColor="#FFFFFF" android:pathData="M19,13v-2c-1.54,0.02 -3.09,-0.75 -4.07,-1.83l-1.29,-1.43c-0.17,-0.19 -0.38,-0.34 -0.61,-0.45 -0.01,0 -0.01,-0.01 -0.02,-0.01L13,7.28c-0.35,-0.2 -0.75,-0.3 -1.19,-0.26C10.76,7.11 10,8.04 10,9.09L10,15c0,1.1 0.9,2 2,2h5v5h2v-5.5c0,-1.1 -0.9,-2 -2,-2h-3v-3.45c1.29,1.07 3.25,1.94 5,1.95zM12.83,18c-0.41,1.16 -1.52,2 -2.83,2 -1.66,0 -3,-1.34 -3,-3 0,-1.31 0.84,-2.41 2,-2.83L9,12.1c-2.28,0.46 -4,2.48 -4,4.9 0,2.76 2.24,5 5,5 2.42,0 4.44,-1.72 4.9,-4h-2.07z"/>
</vector>

这是我的app build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "au.com.angryitguy.testsvg"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        // Stops the Gradle plugin’s automatic rasterization of vectors
        generatedDensities = []
    }
    // Flag to tell aapt to keep the attribute ids around
    aaptOptions {
        additionalParameters "--no-version-vectors"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.0'
}

这是崩溃。 (注意引用textview的膨胀错误。)

03-02 07:56:08.808 13863-13863/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{au.com.angryitguy.testsvg/au.com.angryitguy.testsvg.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class TextView
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
                at android.app.ActivityThread.access$600(ActivityThread.java:130)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
                at android.os.Handler.dispatchMessage(Handler.java:99)
                at android.os.Looper.loop(Looper.java:137)
                at android.app.ActivityThread.main(ActivityThread.java:4745)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                at dalvik.system.NativeStart.main(Native Method)
                                                    Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class TextView
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
                at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267)
                at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
                at au.com.angryitguy.testsvg.MainActivity.onCreate(MainActivity.java:14)
                at android.app.Activity.performCreate(Activity.java:5008)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
                at android.app.ActivityThread.access$600(ActivityThread.java:130) 
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
                at android.os.Handler.dispatchMessage(Handler.java:99) 
                at android.os.Looper.loop(Looper.java:137) 
                at android.app.ActivityThread.main(ActivityThread.java:4745) 
                at java.lang.reflect.Method.invokeNative(Native Method) 
                at java.lang.reflect.Method.invoke(Method.java:511) 
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                at dalvik.system.NativeStart.main(Native Method) 
                                                    Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_accessible_white_36px.xml from drawable resource ID #0x7f02004b
                at android.content.res.Resources.loadDrawable(Resources.java:1918)
                at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
                at android.widget.TextView.<init>(TextView.java:622)
                at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:60)
                at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:56)
                at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
                at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:963)
                at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1022)
                at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:675)
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
                at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
                at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
                at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
                at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267) 
                at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129) 
                at au.com.angryitguy.testsvg.MainActivity.onCreate(MainActivity.java:14) 
                at android.app.Activity.performCreate(Activity.java:5008) 
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
                at android.app.ActivityThread.access$600(ActivityThread.java:130) 
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
                at android.os.Handler.dispatchMessage(Handler.java:99) 
                at android.os.Looper.loop(Looper.java:137) 
                at android.app.ActivityThread.main(ActivityThread.java:4745) 
                at java.lang.reflect.Method.invokeNative(Native Method) 
                at java.lang.reflect.Method.invoke(Method.java:511) 
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                at dalvik.system.NativeStart.main(Native Method) 
                                                    Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector
                at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:877)
                at android.graphics.drawable.Drawable.createFromXml(Drawable.java:818)
                at android.content.res.Resources.loadDrawable(Resources.java:1915)
                at android.content.res.TypedArray.getDrawable(TypedArray.java:601) 
                at android.widget.TextView.<init>(TextView.java:622) 
                at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:60) 
                at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:56) 
                at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103) 
                at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:963) 
                at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1022) 
                at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44) 
                at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:675) 
                at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
                at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
                at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
                at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
                at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267) 
                at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129) 
                at au.com.angryitguy.testsvg.MainActivity.onCreate(MainActivity.java:14) 
                at android.app.Activity.performCreate(Activity.java:5008) 
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
                at android.app.ActivityThread.access$600(ActivityThread.java:130) 
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
                at android.os.Handler.dispatchMessage(Handler.java:99) 
                at android.os.Looper.loop(Looper.java:137) 
                at android.app.ActivityThread.main(ActivityThread.java:4745) 
                at java.lang.reflect.Method.invokeNative(Native Method) 
                at java.lang.reflect.Method.invoke(Method.java:511) 
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                at dalvik.system.NativeStart.main(Native Method) 
trans by 2019-07-12T01:36:01Z

1 共1页