Draw 9 patch dotted /dashed line on Android

空扰寡人 提交于 2019-12-23 03:04:32

问题


Google doesn't help me find a solution of my problem. (As far I was looking for...).

I want to know if it's possible to draw a dotted line with 9 patch capabilities on Android.

My goal is to draw a dotted line from a layout to another one.

I thought about 9 patch because I want the dotted line to be the same in every Activities. (not to be stretched).

Is that possible ? Or maybe is there a better solution ?

Here an example (with a solid line) of what I would like : http://hpics.li/866bb56

Thanks


回答1:


You can't use nine patch for this if you want to have a nice dotted line. The fact that the 9 patch will expand in size will deform your dots and stretch the spaces.

What you want to look into is Shapes and use them as a background :

/res/drawable/dotted.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">

<stroke
    android:color="#CC0000"
    android:dashWidth="10px"
    android:dashGap="10px" /> 
</shape>

and use that as the background of the view.

Learn more : http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape




回答2:


This can be done through XML resources.

Define a drawable with with a stroke (it can be dashed) and set it as your background for example.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#000000" />
<stroke
    android:dashWidth="4dp"
    android:dashGap="4dp"
    android:width="2dp"
    android:color="#FF0000" />
<padding
    android:bottom="4dp"
    android:left="4dp"
    android:right="4dp"
    android:top="4dp" />
</shape>

Resulting in something like this:

Regards, Alex.




回答3:


Actually, i'm not using shape because I need one vertical dotted line and another one horizontal.

And it's not possible to get vertical line with shape. (In fact, it's possible but not natively and the result is not what i'm expecting).

So i took my dotted line, maked 2 small images with it, one horizontal and the another one vertical. Then I created 2 xml file for each orientation and call my drawable with repeat mode

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/pointilles_horizontal_pattern"
android:tileMode="repeat" />

Thanks everyone !



来源:https://stackoverflow.com/questions/11224522/draw-9-patch-dotted-dashed-line-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!