android画虚线后是实线

android4.0之后就默认开启硬件加速,用drawable文件写虚线在4.0之后就不会显示。
    解决方法是我们可以关闭android的硬件加速.

  Application级别:<applicationandroid:hardwareAccelerated="true" ...>

      Activity级别:<activity android:hardwareAccelerated="false" ...>

      Window级别:

getWindow().setFlags(
    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);

      注意:目前为止,Android还不支持在Window级别关闭硬件加速。

      View级别:

myView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
其实是还可以在xml文件里面关闭硬件加速,这是我目前觉得算好的方法。
                            android:layerType="software"
  最好的方法就是自定义一个view。
        
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.view.View;

import com.zy.haoyikang_consumers.R;


/**
* 虚线view
* Created by deng on 16/4/18.
*/
public class DashLineView extends View {

private final int HORIZONTAL = 0, VERTICAL = 1;

private int mOrientation = 1;
private int mDashColor = Color.BLACK;
private int mDashWidth = 10;
private int mWidth = 10;

public DashLineView(Context context) {
this(context, null);
}

public DashLineView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DashLineView, 0, 0);
mOrientation = a.getInt(R.styleable.DashLineView_dlv_orientation, mOrientation);
mDashColor = a.getColor(R.styleable.DashLineView_dlv_dashColor, mDashColor);
mDashWidth = a.getDimensionPixelSize(R.styleable.DashLineView_dlv_dashWidth, mDashWidth);
mWidth = a.getDimensionPixelSize(R.styleable.DashLineView_dlv_width, mWidth);
a.recycle();

}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(mDashColor);
paint.setStrokeWidth(mWidth);
//
// PathEffect effects = new DashPathEffect(new float[]{mDashWidth, mDashWidth, mDashWidth, mDashWidth}, 1);
// paint.setPathEffect(effects);
//
// if(HORIZONTAL == mOrientation)
// canvas.drawLine(0,0,getWidth(),0,paint);
// else canvas.drawLine(0,0,0,getHeight(),paint);

Path path = new Path();
if(mOrientation == HORIZONTAL){
path.moveTo(0, 0);
path.lineTo(getWidth(),0);
}else{
path.moveTo(0,0);
path.lineTo(0,getHeight());
}
PathEffect effects = new DashPathEffect(new float[]{mDashWidth,mDashWidth,mDashWidth,mDashWidth},1);
paint.setPathEffect(effects);
canvas.drawPath(path, paint);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值