最近刚换了公司,博客好久没更新了,最近接触了好多新的技巧,整理下来以免自己忘记!
一、大图解决方案
- ImageView的adjustViewBounds可以保证原始比例,配合maxHeigh,maxWidth才能实现效果,当然匹配父窗体就等于设置了最大了。
- 利用他们的宽高比例自己计算
LayoutParams params = iv.getLayoutParams();
params.width = -1;
params.height = (int) (bitmap.getHeight() * ((float) App.screenWidth/ bitmap.getWidth()));
二、悬浮布局解决方案(类似饿了么商品列表)
- ScrollView利用onScrollChanged方法获取准确的ScrollY值,然后判断要悬浮的列表与自己的控件getTop的大小进行显示与隐藏。
ListView我不知道为什么onScrollChanged这个方法t一直获取的是0,但是我们可以重写 setOnScrollListener方法获取精确的ScrollY值,代码如下:
setOnScrollListener(new AbsListView.OnScrollListener() { private SparseArray recordSp = new SparseArray(0); private int mCurrentfirstVisibleItem = 0; @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { mCurrentfirstVisibleItem = firstVisibleItem; View firstView = view.getChildAt(0); if (null != firstView) { ItemRecod itemRecord = (ItemRecod) recordSp.get(firstVisibleItem); if (null == itemRecord) { itemRecord = new ItemRecod(); } itemRecord.height = firstView.getHeight(); itemRecord.top = firstView.getTop(); recordSp.append(firstVisibleItem, itemRecord); } if (Onscroll!=null) Onscroll.Change(getScrollY()); Log.d("sss", getScrollY() + "::::"); } private int getScrollY() { int height = 0; for (int i = 0; i < mCurrentfirstVisibleItem; i++) { ItemRecod itemRecod = (ItemRecod) recordSp.get(i); height += itemRecod.height; } ItemRecod itemRecod = (ItemRecod) recordSp.get(mCurrentfirstVisibleItem); if (null == itemRecod) { itemRecod = new ItemRecod(); } return height - itemRecod.top; } class ItemRecod { int height = 0; int top = 0; } });
适当位置进行回调处理自己的逻辑即可。
三、阻尼效果解决方案
overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int
scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent)方法,其中的deltaY为关键值,具体自行百度四、历史访问记录解决方案(前端记录)
方案一、使用sqlite数据库,CRUD
方案二、使用SharedPreferences
public static void saveSearchHistory(Context context,String text) {
SharedPreferences sp = context.getSharedPreferences(Constants.SEARCH_HISTORY, 0);
String save_Str = sp.getString("history", "");
String[] hisArrays = save_Str.split(",");
for (int i = 0; i < hisArrays.length; i++) {
if (hisArrays[i].equals(text)) {
return;
}
}
if (hisArrays.length >= 15) {
save_Str = save_Str.substring(0, save_Str.lastIndexOf(","));
}
StringBuilder sb = new StringBuilder(text);
if (!"".equals(save_Str)) {
sb.append("," + save_Str);
}
sp.edit().putString("history", sb.toString()).commit();
}
public static List<String> getSearchHistory(Context context){
List<String> list = new ArrayList<String>();
SharedPreferences sp = context.getSharedPreferences(Constants.SEARCH_HISTORY, 0);
String save_Str = sp.getString("history", "");
if("".equals(save_Str)){
return list;
}
Tools.showCenterToast(context,"save_Str="+save_Str);
String[] hisArrays = save_Str.split(",");
for(int i=0;i<hisArrays.length;i++)
{
list.add(hisArrays[i]);
}
return list;
}
public static void clearSearchHistory(Context context){
SharedPreferences.Editor editor = context.getSharedPreferences(Constants.SEARCH_HISTORY,0).edit();
editor.remove("history");
editor.commit();
}
五、popwindow背景变暗解决方案
方案一、自定义布局,匹配父窗体,内容外用半透明填充,监听miss事件,点击消失
方案二、setAnimationStyle方式:
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_filter, null);
PopupWindow popupWindow = new PopupWindow(view, -1, -2, true);
popupWindow.setAnimationStyle(R.style.ActionSheetDialogAnimation);
// 下边三行的作用是点击空白处的时候PopupWindow会消失
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
popupWindow.showAsDropDown(w);
darkenBackgroud(0.4f);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
darkenBackgroud(1f);
}
});
style代码:
<style name="ActionSheetDialogAnimation">
<item name="android:windowEnterAnimation">@anim/actionsheet_dialog_in</item>
<item name="android:windowExitAnimation">@anim/actionsheet_dialog_out</item>
</style>
in:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="200%"
android:toYDelta="0"
android:duration="500"/>
</set>
out:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 定义从右向左动画退出动画 -->
<translate
android:fromYDelta="0"
android:toYDelta="200%"
android:duration="500"/>
</set
private void darkenBackgroud(Float bgcolor) {//这种效果是背景全部是暗色
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = bgcolor;
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
getWindow().setAttributes(lp);
}
待续 - - -