Skip to content

Instantly share code, notes, and snippets.

@happycodinggirl
Last active June 8, 2016 06:20
Show Gist options
  • Save happycodinggirl/6c20b18ee050ef617e63 to your computer and use it in GitHub Desktop.
Save happycodinggirl/6c20b18ee050ef617e63 to your computer and use it in GitHub Desktop.
LevelListDrawable的简单使用
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ImageView
android:id="@+id/image"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/leveldrawable"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/rect"
android:maxLevel="0" />
<item
android:drawable="@drawable/oval"
android:maxLevel="1" />
</level-list>
package com.lily.huangxingli.testleveldrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LevelListDrawable;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.os.Handler;
public class MainActivity extends ActionBarActivity {
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView= (ImageView) findViewById(R.id.image);
final Drawable drawable=imageView.getDrawable();
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
drawable.setLevel(0);
}
};
if (drawable instanceof LevelListDrawable){
drawable.setLevel(1);
Message message=handler.obtainMessage();
handler.sendMessageDelayed(message, 2000);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<shape
android:shape="oval"
xmlns:android="http://schemas.android.com/apk/res/android">
<size
android:width="30dp"
android:height="30dp"
></size>
<gradient
android:startColor="@android:color/holo_blue_bright"
android:endColor="@android:color/holo_green_light"></gradient>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape
android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:radius="2dp"
></corners>
<gradient android:startColor="@android:color/holo_blue_bright" android:endColor="@android:color/holo_red_light"></gradient>
<size
android:width="30dp"
android:height="30dp"></size>
</shape>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment