drawable 下布局:
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:clipOrientation="vertical"
android:drawable="@drawable/ic_launcher"
android:gravity="bottom"
>
</clip>
主布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.testclipdrawable.MainActivity"
android:id="@+id/viewlayout" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_below="@+id/textView1"
android:layout_marginLeft="35dp"
android:layout_marginTop="182dp"
android:layout_toRightOf="@+id/textView1"
android:src="@drawable/clip_drawable" />
</RelativeLayout>
代码:
public class MainActivity extends ActionBarActivity {
private ImageView mImageView;
private static ClipDrawable clip;
private static final int PROGRESS_CHANGE = 0x2345;
private static int progress = 0;
private static Handler mHandler = new Handler(){
@Override
public void handleMessage(android.os.Message msg) {
switch(msg.what){
case PROGRESS_CHANGE:
progress += 1000;
if(progress >= 10000){
progress = 0;
}
clip.setLevel(progress);
mHandler.sendEmptyMessageDelayed(PROGRESS_CHANGE, 300);
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = ( ImageView) findViewById(R.id.imageView1);
clip = (ClipDrawable) mImageView.getDrawable();
mHandler.sendEmptyMessageDelayed(PROGRESS_CHANGE, 1000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onDestroy(){
super.onDestroy();
mHandler.removeCallbacksAndMessages(null);
}
}
效果:
渐出