【问题标题】:Android Draw line Animation on Image Border onlyAndroid仅在图像边框上绘制线条动画
【发布时间】:2013-05-24 05:35:02
【问题描述】:

我有一个图像视图。我将一个图像从可绘制设置到该图像视图。现在我想在该图像的边框上画线。任何人都可以帮助实现这一目标吗?我使用路径检查它可以完成。我只想以动画方式在该图像的边框上画线...提前致谢..

我正在尝试这样

     Path path = new Path();
    Canvas c = new Canvas();
    path.addRect(view.getLeft(),view.getTop(),view.getRight(),view.getBottom(),Path.Direction.CW);
     Paint p = new Paint();
     p.setColor(Color.GREEN);
     c.drawPath(path, p);

【问题讨论】:

    标签: android path android-canvas draw


    【解决方案1】:

    它会精确生成一条动画线,您只需调整到视图边缘的路径即可创建边框。例如:

    通过视图参数/任意形状定义路径:

    Path path = new Path();
    Canvas c = new Canvas();
     Paint mPaint= new Paint();
    path.addRect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom(), Path.Direction.CW);
    PathEffect pe = new DashPathEffect(new float[] {10, 5, 5, 5}, phase);
    mPaint.setPathEffect(pe);
    c.drawPath(path, mPaint);
    

    或者你可以像这样使用xml

    drawable/dotted.xml:

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    
        <stroke
           android:color="#C7B299"
           android:dashWidth="10px"
           android:dashGap="10px" />
    </shape>
    

    view.xml:

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/dotted" />
    

    【讨论】:

    • 我尝试使用上面相同的代码,但我的屏幕上什么也没有。我只有相同的图像。
    • 路径 path = new Path();画布 c = 新画布();油漆 mPaint=新油漆(); //player是我这里的图片 path.addRect(player.getLeft(), player.getTop(), player.getRight(), player.getBottom(), Path.Direction.CW); PathEffect pe = new DashPathEffect(new float[] {10, 5, 5, 5}, 相位); mPaint.setPathEffect(pe); mPaint.setColor(Color.GREEN); c.drawPath(path, mPaint);
    【解决方案2】:

    对此的简单解决方案是在可绘制文件夹中创建一个形状,其笔触是您想要的颜色和宽度,然后将其作为图像背景,这将显示为图像的边框

    <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="#00000000"/>
        <corners android:radius="10px"/>
        <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" /> 
        <stroke 
            android:width="2px"
            android:color="#ffffff"
            />
    </shape>
    

    现在只需将此drawble设置为布局文件中的图像背景,您将看到图像周围的白色边框

    【讨论】:

    • 我不想设置边框..我想在该图像视图的边框上画线。
    猜你喜欢
    • 2014-07-19
    • 1970-01-01
    • 2013-12-21
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 2014-02-21
    相关资源
    最近更新 更多