Skip to content

Instantly share code, notes, and snippets.

@shau-lok
shau-lok / FixingAddFooterView4.3.md
Created April 14, 2016 03:08
Something wrong about listView.addFooterView() in the preVersion Android 4.4
@shau-lok
shau-lok / UsingShapeDrawable.java
Created April 15, 2016 09:29
ShapeDrawable 用法
ShapeDrawable rectChecked = new ShapeDrawable(new RectShape());
rectChecked.getPaint().setColor(0xfffffff);
rectChecked.getPaint().setStyle(Paint.Style.STROKE);
rectChecked.getPaint().setStrokeWidth(strokeWidth);
rectChecked.setIntrinsicHeight(bitmap.getHeight() + strokeWidth);
rectChecked.setIntrinsicWidth(bitmap.getWidth() + strokeWidth);
Drawable[] drawableArray = new Drawable[]{bmp,rectChecked};
LayerDrawable layerChecked = new LayerDrawable(drawableArray);
@shau-lok
shau-lok / UsingStateListDrawable.java
Created April 15, 2016 09:42
StateListDrawable 用法
//StateList drawable
StateListDrawable states = new StateListDrawable();
states.addState(new int[]{android.R.attr.state_checked},layerChecked);
states.addState(new int[]{},layerUnchecked);
@shau-lok
shau-lok / AndroidVersionCompare.java
Created April 15, 2016 09:53
Android version compare 版本比较
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
setBackground(states);
}else{
setBackgroundDrawable(states);
}
// 查看SDK_INT对应的版本号
// https://developer.android.com/guide/topics/manifest/uses-sdk-element.html
@shau-lok
shau-lok / JavaTransfer2pixel.java
Last active April 18, 2016 02:54
像素转换,using density
int width = 72;
int height = 96;
float scale = getResources().getDisplayMetrics().density;
int pixelsWidth = (int) (width * scale + 0.5f);
int pixelsHeight = (int) (height * scale + 0.5f);
@shau-lok
shau-lok / AndroidVersionCompare.java
Created April 18, 2016 06:15
Android版本号比较
public static int compareVersion(String version1, String version2) {
if (TextUtils.isEmpty(version1)) {
return -1;
}
if (TextUtils.isEmpty(version2)) {
return -1;
}
if (!TextUtils.isEmpty(version1) && !TextUtils.isEmpty(version2)) {
try {
@shau-lok
shau-lok / InsertArrayList.java
Created May 3, 2016 11:07
ArrayList move specific item in array list to the first item
if (Util.getListSize(hotellist) > 0) {
int index = hotellist.indexOf(h);
hotellist.remove(index);
hotellist.add(0, h);
}
@shau-lok
shau-lok / NineOldAndroid.md
Created May 3, 2016 11:13
NineOldAndroid动画使用

NineOldAndroid demo

官网介绍

ObjectAnimator.ofFloat(myObject, "translationY", -myObject.getHeight()).start();
ValueAnimator colorAnim = ObjectAnimator.ofInt(this, "backgroundColor", /*Red*/0xFFFF8080, /*Blue*/0xFF8080FF);
@shau-lok
shau-lok / setListViewHeightBasedOnItems.java
Created May 3, 2016 11:20
ListView 设置高度根据item高度
/**
* Sets ListView height dynamically based on the height of the items.
*
* @param listView to be resized
* @return true if the listView is successfully resized, false otherwise
*/
public static boolean setListViewHeightBasedOnItems(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter != null) {
@shau-lok
shau-lok / getViewHeight.java
Created May 3, 2016 11:21
获取View的高度
/**
* 计算view高度
*
* @param v
* @return
*/
public static int getViewHeight(View v) {
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(Constants.DEVICE_WIDTH, View.MeasureSpec.EXACTLY);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
v.measure(widthMeasureSpec, heightMeasureSpec);