您的位置:首页 > 移动开发 > Android开发

LevelListDrawable的使用

2016-12-08 14:48 429 查看
LevelListDrawable对应level-list 标签 表示一个Drawable集合,每个drawable都有一个level,在代码中可以根据不同等级切换不同的drawable

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@mipmap/picture"
android:minLevel="1"
android:maxLevel="10"/>

<item
android:drawable="@mipmap/img"
android:minLevel="11"
android:maxLevel="20"/>
</level-list>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
android:id="@+id/imge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:src="@drawable/level_list_drawable"/>

<Button
android:id="@+id/bt_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="下一张"/>
</LinearLayout>


MainActivity.java

package com.zhoujian.drawable;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity
{
private int mLevel;
private ImageView imge;
private Button mButton;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imge = (ImageView) findViewById(R.id.imge);
imge.setImageLevel(10);

mButton = (Button) findViewById(R.id.bt_button);

mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{

mLevel= imge.getDrawable().getLevel();

if (mLevel==10)
{
imge.setImageLevel(20);
}
else
{
imge.setImageLevel(10);
}
}
});

}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android