您的位置:首页 > 移动开发 > Android开发

android画虚线后是实线

2016-04-19 15:58 363 查看
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: