标签: android-drawable

如何从drawable引用样式属性?

我想为我的应用程序提供2个可选主题.为了做到这一点,我定义了一些属性,如下所示:

 <attr format="color" name="item_background" />
Run Code Online (Sandbox Code Playgroud)

然后,我创建了两个主题,如下所示:

  <style name="ThemeA">
     <item name="item_background">#123456</item>
 </style>

 <style name="ThemeB">
     <item name="item_background">#ABCDEF</item>
 </style>
Run Code Online (Sandbox Code Playgroud)

这种方法效果很好,允许我轻松地创建和修改几个主题.问题是它似乎只能在Views中使用,而不能在Drawables中使用.

例如,引用布局内View的值可以:

 <TextView android:background="?item_background" />
Run Code Online (Sandbox Code Playgroud)

但是在Drawable中做同样的事情并不是:

 <shape android:shape="rectangle">
     <solid android:color="?item_background" />
 </shape>
Run Code Online (Sandbox Code Playgroud)

运行应用程序时出现此错误:

    java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
Run Code Online (Sandbox Code Playgroud)

如果不是?item_background我使用硬编码颜色,它可以工作,但这不允许我使用我的主题.我也试过?attr:item_background,但同样的事情发生了.

我怎么能这样做?为什么它在Views中有效但在Drawables中无效?我在文档中的任何地方都找不到这个限制......

android android-theme android-drawable android-attributes

87
推荐指数
4
解决办法
3万
查看次数

如何在不进行缩放的情况下将图层列表中的矢量绘图居中

我试图VectorDrawableLayerList没有缩放矢量的情况下使用a .例如:

<layer-list>
    <item android:drawable="@color/grid_item_activated"/>
    <item android:gravity="center" android:drawable="@drawable/ic_check_white_48dp"/>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

drawable ic_check_white_48dpid定义为:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="48dp"
        android:height="48dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <path
        android:fillColor="#FFFFFFFF"
        android:pathData="M9,16.17L4.83,12l-1.42,1.41L9,19 21,7l-1.41,-1.41z"/>
</vector>
Run Code Online (Sandbox Code Playgroud)

所需的效果是检查图标在图层可绘制的中心,而不进行缩放.问题是,上面的图层列表会导致检查图标缩放以适合图层大小.

如果我用每个密度的PNG替换矢量drawable并修改图层列表,我可以产生所需的效果:

<layer-list>
    <item android:drawable="@color/grid_item_activated"/>
    <item>
        <bitmap android:gravity="center" android:src="@drawable/ic_check_white_48dp"/>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

我有什么方法可以使用VectorDrawable

android layer-list android-drawable android-vectordrawable

87
推荐指数
4
解决办法
3万
查看次数

画布上的可绘制图像

如何在画布上获取图像以便在该图像上绘图?

android android-canvas android-drawable

86
推荐指数
5
解决办法
15万
查看次数

在前Lollipop崩溃时使用Android矢量Drawables

我在Lollipop之前使用android中的矢量drawables,这些是我的一些库和工具版本:

  • Android Studio:2.0
  • Android Gradle插件:2.0.0
  • 构建工具:23.0.2
  • Android支持库:23.3.0

我在我的应用级别添加了此属性 Build.Gradle

android {  
  defaultConfig {  
    vectorDrawables.useSupportLibrary = true  
   }  
}
Run Code Online (Sandbox Code Playgroud)

还值得一提的是,我使用了一个额外的drawable,如Android官方博客(此处链接)中所述的LayerDrawable(layer_list),用于设置外部矢量绘图的drawablesapp:srcCompat

<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/search"/>
</level-list>
Run Code Online (Sandbox Code Playgroud)

你会发现在app之外直接引用矢量drawable:srcCompat将在Lollipop之前失败.但是,AppCompat确实支持在另一个可绘制容器(如StateListDrawable,InsetDrawable,LayerDrawable,LevelListDrawable和RotateDrawable)中引用它们时加载矢量drawable.通过使用此 间接,您可以在TextView的android:drawableLeft属性中使用向量drawable,这通常不能支持向量drawable.

当我使用的app:srcCompat一切工作正常,但当我使用时:

android:background
android:drawableLeft
android:drawableRight
android:drawableTop
android:drawableBottom
Run Code Online (Sandbox Code Playgroud)

ImageView,ImageButton,TextViewEditText到棒棒糖之前,它引发厚望:

Caused by: android.content.res.Resources$NotFoundException: File res/drawable/search_toggle.xml from drawable resource ID #0x7f0200a9
Run Code Online (Sandbox Code Playgroud)

android vector android-compatibility android-support-library android-drawable

81
推荐指数
9
解决办法
5万
查看次数

不同分辨率支持android

编辑问题:

移动分辨率:
我想设计不同的屏幕dpi,如下面的分辨率.
小320x480,
480×800,
540x960,
720x1280(三星S3),
1080×1920(S4,Nexus5,纳克斯5X,摩托G4),
2560 X 1440(的Nexus 6,纳克斯6P,三星边缘)

平板分辨率:
480×800(Micromax的),
600x1024(三星tab2),
800x1280(nexus 7),
1200x1920(新nexus 7),
2048x1536(nexus 9)

我想根据设备显示分辨率使用不同的字体大小.

Q1)best解决这个问题的方法是什么problem

Q2)什么是抛出编码或XML的最佳选择?

Q3)哪个drawable文件夹代表哪个设备分辨率?

Q4)应用程序启动器图标大小为不同的分辨率?

android screen-resolution android-custom-view android-xml android-drawable

78
推荐指数
5
解决办法
9万
查看次数

Android:context.getDrawable()的替代方案

context.getDrawable()在我的项目中使用过这样的:

Drawable greenProgressbar = context.getDrawable(R.drawable.custom_progressbargreen);
Run Code Online (Sandbox Code Playgroud)

但是Eclipse给了我一个它需要的错误Minimum API level of 21.这意味着在快速谷歌搜索后,我的APP将只能使用Android 5.0.由于并非所有设备都使用此版本的Android,我想有一个替代品context.getDrawable().

java eclipse android android-drawable

77
推荐指数
4
解决办法
6万
查看次数

从文件路径创建位图/ Drawable

我正在尝试从现有文件路径创建一个Bitmap或Drawable.

String path = intent.getStringExtra("FilePath");
BitmapFactory.Options option = new BitmapFactory.Options();
option.inPreferredConfig = Bitmap.Config.ARGB_8888;

mImg.setImageBitmap(BitmapFactory.decodeFile(path));
// mImg.setImageBitmap(BitmapFactory.decodeFile(path, option));
// mImg.setImageDrawable(Drawable.createFromPath(path));
mImg.setVisibility(View.VISIBLE);
mText.setText(path);
Run Code Online (Sandbox Code Playgroud)

但是setImageBitmap(),setImageDrawable()没有显示路径中的图像.我打印了路径,mText它看起来像:/storage/sdcard0/DCIM/100LGDSC/CAM00001.jpg

我究竟做错了什么?有人可以帮帮我吗?

java android bitmapfactory android-drawable

76
推荐指数
3
解决办法
14万
查看次数

向ListView最后一个元素添加底部边距

我需要添加添加具有复杂项目背景的ListView:顶部和底部的偶数/奇数和圆角不同.它看起来像这样:

ListView顶部

我已经通过level-list实现了所有这些东西,但还有一件事我想做.现在底部项目靠近屏幕的底部.最好添加一些空间.

ListView底部看起来不太好

我不想为ListView添加底部边距,我只需要最后一项的保证金.

我看到这样做的方式:

页脚

一种破解 - 将带有空TextView的页脚添加到ListView.但是页脚是非常不稳定的东西,它们通常会在notifyDataSetChanged之后消失,并且无法让它们恢复

带透明像素的图像

我让设计师向底部背景资源添加透明像素.不幸的是,在这种情况下,垂直居中完全被打破.例如,有9patch像这样:

9patch

和这样的布局:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
        >
    <!-- View with background with transparent pixels on bottom -->
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
                  android:id="@+id/item"
                  android:background="@drawable/some_bgr"
                  android:padding="10dp"
            >
        <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"
                  android:text="Title"
                  android:layout_gravity="center"
                  android:textSize="18sp"
                />
        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                  android:text="Detail"
                  android:layout_gravity="center"
                  android:textSize="18sp"
                />
    </LinearLayout>

    <!-- Just for marking place took by view -->
    <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"
                 android:layout_below="@id/item"
                 android:background="#88ff55"
            />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

结果:

结果

如您所见,居中不起作用.不幸.(顺便说一句,如果将此9patch指定为TextView的背景,则居中工作效果很好.如果你知道任何文章,请解释一下,请告诉我.)

在Adapter实现中添加最后一项的下边距

这应该工作,但由于未知的原因,我仍然无法使它工作.我不喜欢这种方式,因为我不喜欢修改代码中的维度.

所以

已经存在想象的方式 - 使用特定的位图和边距构造一些XML drawable.根据drawables概念应该是可能的,但我找不到实现.可能有人知道吗?

还有其他想法吗?

android centering android-drawable

76
推荐指数
2
解决办法
2万
查看次数

Android中的Bitmap和Drawable有什么区别?

我用谷歌搜索,但我找不到任何文章来描述Android中的Bitmap和Drawable之间的区别.

android terminology android-drawable android-bitmap

70
推荐指数
4
解决办法
5万
查看次数

Android Studio可绘制文件夹

在Android Studio中,我无法确定在应用程序中放置图像的位置.drawable文件夹没有分解为drawable-hdpi,drawable-ldpi等.我看到了另一个问题,答案是切换到Project视图而不是Android视图,但drawable只有一个文件夹.有mipmap-hdpi,mipmap-ldpi等,但人们都说这只适用于app图标.我糊涂了.

android android-drawable android-studio

70
推荐指数
5
解决办法
11万
查看次数