1.透明式狀態(tài)欄
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//使?fàn)顟B(tài)欄透明
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// 如果想要給狀態(tài)欄加點(diǎn)顏色,例如加點(diǎn)透明的陰影舒萎,就需要加上下面的三行代碼
// 這是生成一個(gè)狀態(tài)欄大小的矩形骡男,給這個(gè)矩形添加顏色惦积,添加 statusView 到布局中
View statusView = createStatusView(color);
ViewGroup decorView = (ViewGroup) this.getWindow().getDecorView();
decorView.addView(statusView);
// 設(shè)置根布局的參數(shù)
ViewGroup rootView = (ViewGroup) ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
rootView.setFitsSystemWindows(false);
rootView.setClipToPadding(true);
}
/**
*這個(gè)生成一個(gè)狀態(tài)欄大小的矩形接癌,給這個(gè)矩形,添加 statusView 到布局中
* @param color
* @return
*/
private View createStatusView(int color) {
// 獲得狀態(tài)欄高度
int resourceId = this.getResources().getIdentifier("status_bar_height", "dimen", "android");
int statusBarHeight = this.getResources().getDimensionPixelSize(resourceId);
// 繪制一個(gè)和狀態(tài)欄一樣高的矩形
View statusView = new View(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
statusBarHeight);
statusView.setLayoutParams(params);
statusView.setBackgroundColor(color);
return statusView;
}
圖片一與圖片二差別就是rootView.setFitsSystemWindows(false);圖一是false ,圖二是true
2.圖片的縮放
主要代碼:
package com.example.sunxiaokun.status;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.example.sunxiaokun.testcoordinatorlayout.R;
/**
* Created by sunxiaokun on 2016/8/31.
*/
public class TranslucationActivity extends Activity {
private RecyclerView mAnim_rv;
private ImageView img_mine_background;
// 記錄首次按下位置
private float mFirstPosition = 0;
// 是否正在放大
private Boolean mScaling = false;
private DisplayMetrics metric;
private TransAdapter mTransAdapter;
private LinearLayoutManager mLinearLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_translution);
setColor(Color.parseColor("#33000000"));
mAnim_rv = (RecyclerView) findViewById(R.id.rcv_trans);
if(mLinearLayoutManager == null){
mLinearLayoutManager = new LinearLayoutManager(this);
}
mAnim_rv.setLayoutManager(mLinearLayoutManager);
if (mTransAdapter == null)
mTransAdapter = new TransAdapter(this);
mAnim_rv.setAdapter(mTransAdapter);
mTransAdapter.setOnTouchClick(new OnTouchClick() {
@Override
public void onTouch(ImageView view) {
img_mine_background = view;
metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) img_mine_background.getLayoutParams();
lp.width = metric.widthPixels;
lp.height = metric.widthPixels * 9 / 16;
img_mine_background.setLayoutParams(lp);
initzoomimage();
}
});
}
/**
* 這里我設(shè)置的圖片在狀態(tài)欄的下面规肴,并且給狀態(tài)欄設(shè)置了一個(gè)淺淺的透明色侯勉。
*
* @param color
*/
public void setColor(int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//使?fàn)顟B(tài)欄透明
this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// 如果想要給狀態(tài)欄加點(diǎn)顏色鹦筹,例如加點(diǎn)透明的陰影,就需要加上下面的三行代碼
// 這是生成一個(gè)狀態(tài)欄大小的矩形址貌,給這個(gè)矩形添加顏色铐拐,添加 statusView 到布局中
View statusView = createStatusView(color);
ViewGroup decorView = (ViewGroup) this.getWindow().getDecorView();
decorView.addView(statusView);
// 設(shè)置根布局的參數(shù)
ViewGroup rootView = (ViewGroup) ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
// 這個(gè)是決定我們的布局是否是在狀態(tài)欄的下面
rootView.setFitsSystemWindows(false);
rootView.setClipToPadding(true);
}
}
/**
*這個(gè)生成一個(gè)狀態(tài)欄大小的矩形,給這個(gè)矩形芳誓,添加 statusView 到布局中
* @param color
* @return
*/
private View createStatusView(int color) {
// 獲得狀態(tài)欄高度
int resourceId = this.getResources().getIdentifier("status_bar_height", "dimen", "android");
int statusBarHeight = this.getResources().getDimensionPixelSize(resourceId);
// 繪制一個(gè)和狀態(tài)欄一樣高的矩形
View statusView = new View(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
statusBarHeight);
statusView.setLayoutParams(params);
statusView.setBackgroundColor(color);
return statusView;
}
/**
* 圖片縮放的處理
*/
private void initzoomimage() {
mAnim_rv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) img_mine_background.getLayoutParams();
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
mScaling = false;
replyImage();
break;
case MotionEvent.ACTION_MOVE:
if (!mScaling) {
//當(dāng)圖片也就是第一個(gè)item完全可見的時(shí)候余舶,記錄觸摸屏幕的位置
if (mLinearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0) {
mFirstPosition = event.getY();
} else {
break;
}
}
int distance = (int) ((event.getY() - mFirstPosition)* 0.6); // 滾動(dòng)距離乘以一個(gè)系數(shù)
if (distance < 0) {
break;
}
// 處理放大
mScaling = true;
lp.width = metric.widthPixels + distance;
lp.height = (metric.widthPixels + distance) * 9 / 16;
img_mine_background.setLayoutParams(lp);
return true; // 返回true表示已經(jīng)完成觸摸事件啊鸭,不再處理
}
return false;
}
});
}
private void replyImage() {
final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) img_mine_background.getLayoutParams();
final float w = img_mine_background.getLayoutParams().width;// 圖片當(dāng)前寬度
final float h = img_mine_background.getLayoutParams().height;// 圖片當(dāng)前高度
final float newW = metric.widthPixels;// 圖片原寬度
final float newH = metric.widthPixels * 9 / 16;// 圖片原高度
// 設(shè)置動(dòng)畫
ValueAnimator anim = ObjectAnimator.ofFloat(0.0F, 1.0F).setDuration(200);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float cVal = (Float) animation.getAnimatedValue();
lp.width = (int) (w - (w - newW) * cVal);
lp.height = (int) (h - (h - newH) * cVal);
img_mine_background.setLayoutParams(lp);
}
});
anim.start();
}
}
recyclerview 的adapter:
package com.example.sunxiaokun.status;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.example.sunxiaokun.testcoordinatorlayout.R;
/**
* Created by sunxiaokun on 2016/8/24.
*/
public class TransAdapter extends RecyclerView.Adapter<TransAdapter.ViewHolder> {
private static final int IS_HEADER = 0;
private static final int IS_NORMAL = 1;
private Context mContext;
private OnTouchClick mOnTouchClick;
public void setOnTouchClick(OnTouchClick onTouchClick) {
mOnTouchClick = onTouchClick;
}
public TransAdapter(Context context){
this.mContext = context;
}
@Override
public int getItemViewType(int position) {
return position == 0 ? IS_HEADER : IS_NORMAL;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ViewHolder viewHolder = null;
switch (viewType){
case IS_HEADER:
View view = View.inflate(mContext, R.layout.item_iv,null);
viewHolder = new ViewHolder(view);
viewHolder.trans_iv = (ImageView) view.findViewById(R.id.trans_iv_item);
break;
case IS_NORMAL:
View view1 = View.inflate(mContext, R.layout.item_anim,null);
viewHolder = new ViewHolder(view1);
viewHolder.iv_anim = (ImageView) view1.findViewById(R.id.iv_anim);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
if(position == 0){
mOnTouchClick.onTouch(holder.trans_iv);
}
}
@Override
public int getItemCount() {
return 30;
}
class ViewHolder extends RecyclerView.ViewHolder{
private ImageView iv_anim;
public ImageView trans_iv;
public ViewHolder(View itemView) {
super(itemView);
}
}
}