【问题标题】:Drawing dashed line on Android programmatically以编程方式在Android上绘制虚线
【发布时间】:2015-12-30 18:00:03
【问题描述】:

我想在以编程方式生成的 TextView 之间绘制水平虚线。我试过这段代码:

Paint fgPaintSel = new Paint();
fgPaintSel.setARGB(255, 0, 0, 0);
fgPaintSel.setStyle(Paint.Style.STROKE);
fgPaintSel.setPathEffect(new DashPathEffect(new float[]{5, 10}, 0));

但是什么也没发生。我只是复制并粘贴了这段代码。我应该怎么做才能画一条虚线?谢谢。

【问题讨论】:

  • 尝试调用canvas.drawPath(mPath, fgPaintSel);之后的方法
  • @NKushwah 它无法解析canvasmPath。谢谢。
  • 发布该类或方法的全部代码。
  • @NKushwah 我发布了整个代码。这就是我所有的绘图代码。
  • 你重写了 onDraw() 吗?

标签: java android android-layout paint


【解决方案1】:

给活动布局一个id。我在这个演示中使用了一个按钮和一个 onclick 处理程序 PaintDashedLines()。

在 content_main.xml 布局中。

<LinearLayout android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" .../>

    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="PaintDashedLines"
        android:text="Press Me"/>
</LinearLayout>

出于演示的目的,使用静态 int 进行计数,并使用单独的方法来创建可绘制对象,以实现模块化。

在你的活动中:

static int tvCount = 0;

public void PaintDashedLines(View v) {
    LinearLayout ll = (LinearLayout) findViewById(R.id.main);
    TextView tv = new TextView(MainActivity.this);
    tv.setGravity(Gravity.CENTER);
    tv.setTextSize(25);
    tv.setPadding(0, 5, 0, 5);
    ll.addView(tv);
    tv.setText("TextView " + tvCount);
    ImageView divider = new ImageView(MainActivity.this);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            ll.getWidth(), 2);
    lp.setMargins(0, 5, 0, 5);
    divider.setLayoutParams(lp);
    divider.setBackground(CreateDashedLined());
    ll.addView(divider);
    tvCount++;
}

public static Drawable CreateDashedLined() {
    ShapeDrawable sd = new ShapeDrawable(new RectShape());
    Paint fgPaintSel = sd.getPaint();
    fgPaintSel.setColor(Color.BLACK);
    fgPaintSel.setStyle(Paint.Style.STROKE);
    fgPaintSel.setPathEffect(new DashPathEffect(new float[]{5, 10}, 0));
    return sd;
}

-我的活动
--int 计数;
--oncreate
--PaintDashedLines(查看 v)
--public static Drawable CreateDashedLined()

在 build.gradle 中(虽然这不是一成不变的)

    minSdkVersion 18
    targetSdkVersion 23

您无需执行任何其他操作。

【讨论】:

  • 感谢您的代码。我正在使用您的代码: 1. 注释掉 TextView 行。 2. 将整个 Java 代码粘贴到 Activity 类之上。 3. 拨打View v = new View(this); PaintDashedLines(v); 我想要虚线的地方。但是没有出现虚线。你能告诉我我做错了什么吗?谢谢。
  • 不注释TextView行时,可以看到TextView和数字,但是看不到虚线。
【解决方案2】:

在drawable文件夹中创建一个dotted_line.xml文件:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:left="-3px"
        android:right="-3px"
        android:top="-3px">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">

            <stroke
                android:width="2px"
                android:color="@color/dark_blue"
                android:dashGap="2px"
                android:dashWidth="3px" />
        </shape>
    </item>

</layer-list>

将此可绘制对象添加为背景:

view.setBackground(getResources().getDrawable(R.drawable.dotted_line));

结果:

【讨论】:

  • 像魅力一样工作,谢谢!有一个问题。当 textView 很短时,线条也很短,我希望线条一直都是全长的。我怎样才能做到这一点?谢谢。
  • @jason Possibilities: 1. 设置视图的固定宽度,2. 设置视图的最小宽度,3. 设置视图的左右内边距,4. 将其放入布局中并添加虚线到布局
  • 最后一个问题:如何使点更像虚线?谢谢
  • android:dashGap - 更大的数字 -> 更大的行间距,dashWidth -> 单行宽度。希望对您有所帮助。
【解决方案3】:

在drawable中创建dashed_line.xml

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

  <stroke
     android:color="#C7B299"
     android:dashWidth="10px"
     android:dashGap="10px"
     android:width="1dp"/>
</shape>

以编程方式添加View

View v = new View(this);

并设置其background

v.setBackgroundResource(R.drawable.dashed_line);

根据需要设置Viewheightwidth

【讨论】:

  • @jason 有什么问题?你得到输出还是什么?
  • 没有输出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-06
  • 1970-01-01
相关资源
最近更新 更多