Android自定义View控件实现刷新效果

Android自定义View控件实现刷新效果

2023年7月15日发(作者:)

Android⾃定义View控件实现刷新效果三种得到LinearInflater的⽅法a. LayoutInflater inflater = getLayoutInflater();b. LayoutInflater localinflater =(LayoutInflater)temService(_INFLATER_SERVICE);c. LayoutInflater inflater = (context);onDraw ⽅法绘图,invalidate刷新界⾯。效果图:点击⼀下换颜⾊onDraw画完图后,给控件设置点击事件 ,将参数传到控件⾥,然后invalidate刷新画图,并增加changeColor⽅法public class CusView3 extends View {

private int color = 0;

public CusView3(Context context, AttributeSet attrs) {

super(context, attrs);

}

@Override

protected void onDraw(Canvas canvas) {

(canvas);

Paint mPaint = new Paint();

if (color > 2) {

color = 0;

}

switch (color) {

case 0:

or();

break;

case 1:

or();

break;

case 2:

or();

break;

default:

break;

}

le();

tSize(35.0f);

xt("点击我刷新", 10, 60, mPaint);

}

public void changeColor() { //为了让外⾯调⽤

color++;

}

}2.布局

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:id="@+id/cusview3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

>

3.画图后 给控件设置点击事件 ,将参数传到控件⾥,然后invalidate刷新public class TestCustomViewActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

te(savedInstanceState);

setContentView();

view3 = (CusView3) findViewById(w3);

// 点击事件

lickListener(new kListener() {

@Override

public void onClick(View v) {

Message message = new Message();

= 1;

ssage(message);

}

});

}

Handler myHandler = new Handler() {

// 接收到消息后处理

public void handleMessage(Message msg) {

switch () {

case 1:

// 调⽤⽅法

Color();

// 刷新⽅法

date();

break;

}

Message(msg);

}

};

private CusView3 view3;

}⾄于⾃定义控件占整屏的问题,可能需要⽤layoutparams以上所述是⼩编给⼤家介绍的Android⾃定义View控件实现刷新效果,希望对⼤家有所帮助,如果⼤家有任何疑问欢迎给我留⾔,⼩编会及时回复⼤家的!

发布者:admin,转转请注明出处:http://www.yc00.com/news/1689430992a247120.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信