//要用到clipdrawble,xml功能如下,在drawble目录下新建,类型选择为clip(注意AS不会提示)<?xml version="1.0" encoding="utf-8"?> <clip xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/a" android:clipOrientation="horizontal" android:gravity="center"> </clip> //IMG的XML
<ImageView android:id="@+id/img" android:src="@drawable/clip" android:layout_width="wrap_content" android:layout_height="wrap_content" /> //JAVA代码如下
import android.graphics.drawable.ClipDrawable;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.ImageView; public class MainActivity extends AppCompatActivity { private ImageView img; private ClipDrawable clip; private MyHandler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { img = (ImageView) findViewById(R.id.img); clip=(ClipDrawable) img.getDrawable(); handler=new MyHandler(); MyThread thread=new MyThread(); new Thread(thread).start(); } private class MyThread implements Runnable{ @Override public void run() { while(true){ try { Message msg=handler.obtainMessage(); msg.what=0x123; msg.sendToTarget(); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }} private class MyHandler extends Handler{ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what==0x123){ clip.setLevel(clip.getLevel()+100); } } }}
时间: 2024-11-05 23:36:31