** 自定义Text**
继承 extends View
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="HanText">
<attr name="text" format="string"></attr>
<attr name="daxia" format="dimension"></attr>
<attr name="yanse" format="color"></attr>
</declare-styleable>
</resources>
String wenzi;
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
//java创建对象的时候
public HanText(Context context) {
super(context);
}
//在xml里引用的时候,会自动调用
public HanText(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.HanText);
wenzi = typedArray.getString(R.styleable.HanText_text);
//获取文字大小
dimension = typedArray.getDimension(R.styleable.HanText_daxia, 100);
//获取文字颜色
color = typedArray.getColor(R.styleable.HanText_yanse, Color.RED);
//释放资源
typedArray.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthmode = MeasureSpec.getMode(widthMeasureSpec);
int widthsize = MeasureSpec.getSize(widthMeasureSpec);
switch (widthmode){
case MeasureSpec.EXACTLY://父级位置
break;
case MeasureSpec.AT_MOST://子级位置
with=200;
break;
case MeasureSpec.UNSPECIFIED://不限定位置 比如listview
break;
}
int heighmode = MeasureSpec.getMode(heightMeasureSpec);
int heighsize = MeasureSpec.getSize(heightMeasureSpec);
switch (heighmode){
case MeasureSpec.EXACTLY:
break;
case MeasureSpec.AT_MOST:
height=200;
break;
case MeasureSpec.UNSPECIFIED:
break;
}
setMeasuredDimension(with,height);
}
//具体画出来你的值
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setTextSize(dimension);
paint.setColor(color);
canvas.drawText(wenzi,0,100,paint);
}
** 自定义ViewGroup**
extends ViewGroup
private static final String TAG = "Mygrowp";
public Mygrowp(Context context) {
super(context);
}
public Mygrowp(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//除了 布局都要加位置
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
for (int i = 0; i < getChildCount(); i++) {
View childAt = getChildAt(i);
measureChild(childAt,widthMeasureSpec,heightMeasureSpec);
}
}
int left;//左
int top;//上
int dqcd;//控件的长度
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
for (int i = 0; i <getChildCount() ; i++) {
View childAt = getChildAt(i);
if (i==0){
childAt.layout(left,top,childAt.getMeasuredWidth(),childAt.getMeasuredHeight());
dqcd=childAt.getMeasuredWidth();
left=childAt.getMeasuredWidth();
continue;
}
Log.i(TAG, "onLayout: "+dqcd);
dqcd+=childAt.getMeasuredWidth();//控件长度累加
if (dqcd<getMeasuredWidth()){//长度是否大于布局长度
childAt.layout(left,top,childAt.getMeasuredWidth()+left,childAt.getMeasuredHeight()+top);
left=childAt.getMeasuredWidth()+left;
}else {
left=0;
dqcd=childAt.getMeasuredWidth();//控件总长度初始化
top+=childAt.getMeasuredHeight();//另起一行后高发生改变
childAt.layout(left,top,childAt.getMeasuredWidth()+left,childAt.getMeasuredHeight()+top);
left+=childAt.getMeasuredWidth();//另起一行后继续在后面放
}
}
}
自定义上拉下拉
extends LinearLayout
LinearLayout linearLayout;
LayoutParams layoutParams;
private PointF pointF=new PointF();//存放xy的值
private PointF pointF=new PointF();//存放xy的值
public Myxiala(Context context) {
super(context);
init();
}
public Myxiala(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public void init(){
//创建线性布局
linearLayout = new LinearLayout(getContext());
//给线性布局设置长宽
layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200);
//显示的布局消失在顶部
layoutParams.topMargin=-200;
//加载设置好的宽度
linearLayout.setLayoutParams(layoutParams);
//设置颜色
linearLayout.setBackgroundColor(Color.RED);
//加载布局
addView(linearLayout);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){//判断单击事件
case MotionEvent.ACTION_DOWN:
pointF.x=event.getX();
pointF.y=event.getY();//将点下的坐标给对象存起来
break;
case MotionEvent.ACTION_MOVE:
layoutParams = (LayoutParams) linearLayout.getLayoutParams();//获取拖动的布局
layoutParams.topMargin+=event.getY()-pointF.y;//一像素的往下拖动
if (layoutParams.topMargin>0){
layoutParams.topMargin=0;
}else if (layoutParams.topMargin<-200){
layoutParams.topMargin=-200;
}
linearLayout.setLayoutParams(layoutParams);//赋值设置好的外边距
pointF.x=event.getX();
pointF.y=event.getY();//101-100
break;
case MotionEvent.ACTION_UP:
/*layoutParams = (LayoutParams) linearLayout.getLayoutParams();
if (layoutParams.topMargin>-50){
layoutParams.topMargin=-200;
}else if (layoutParams.topMargin<-50){
layoutParams.topMargin=0;
}
linearLayout.setLayoutParams(layoutParams);*/
if (layoutParams.topMargin>-100){//判断滑动的的位置进行动画
startAnim(layoutParams.topMargin,-200);
}else {
startAnim(layoutParams.topMargin,0);
}
break;
}
return true;
}
private void startAnim(int y, int i) {
View view;
ValueAnimator valueAnimator = ValueAnimator.ofInt(y, i);//将方法的参数放里面
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {//监听器值的属性
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Integer in = (Integer) animation.getAnimatedValue();//强转int
layoutParams = (LayoutParams) linearLayout.getLayoutParams();
layoutParams.topMargin=in;//进行赋值
linearLayout.setLayoutParams(layoutParams);
}
});
valueAnimator.start();//设置动画
}
Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
Paint painttext=new Paint(Paint.ANTI_ALIAS_FLAG);
Paint painthu=new Paint(Paint.ANTI_ALIAS_FLAG);
int time=6;
int prant;
RectF rectF = new RectF(200, 500, 600, 900);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(15);
canvas.drawCircle(400,700,200,paint);
painttext.setTextSize(50);
painttext.setColor(Color.RED);
canvas.drawText(time+"",rectF.centerX(),rectF.centerY(),painttext);
painthu.setColor(Color.GREEN);
painthu.setStyle(Paint.Style.STROKE);
painthu.setStrokeWidth(15);
canvas.drawArc(rectF,270,prant,false,painthu);
public void start(){
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <=360 ; i++) {
prant=i;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (i%72==0){
time--;
}
Mycric.this.postInvalidate();
}
}
}).start();
}