Skip to content

Commit

Permalink
shift模式的完成 向下兼容点击变色 向下兼容阴影效果
Browse files Browse the repository at this point in the history
  • Loading branch information
WakeHao committed Jan 17, 2017
1 parent c1546b7 commit 484052a
Show file tree
Hide file tree
Showing 21 changed files with 800 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions app/src/main/java/com/wakehao/demo/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
package com.wakehao.demo;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.wakehao.bar.BottomNavigationBar;
import com.wakehao.bar.BottomNavigationItem;

public class MainActivity extends AppCompatActivity {

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

BottomNavigationBar bar= (BottomNavigationBar) findViewById(R.id.bar);

// bar.setOnNavigationItemSelectedListener(new BottomNavigationBar.OnNavigationItemSelectedListener() {
// @Override
// public boolean onNavigationItemSelected(@NonNull BottomNavigationItem item, int selectedPosition) {
// return selectedPosition==2?false:true;
// }
// });
}
}
Binary file added app/src/main/res/drawable-xhdpi/ic_home.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xhdpi/ic_t1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xhdpi/ic_t2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xhdpi/ic_t3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xhdpi/ic_t4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xhdpi/ic_t5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
tools:context="com.wakehao.demo.MainActivity">

<com.wakehao.bar.BottomNavigationBar
android:id="@+id/bar"
app:switchMode="shift"
app:menu="@menu/demo_menu"

android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="56dp">

Expand Down
14 changes: 10 additions & 4 deletions app/src/main/res/menu/demo_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@
<item
android:id="@+id/test_1"
android:title="@string/home"
android:icon="@mipmap/ic_launcher"
android:icon="@drawable/ic_home"
></item>
<item
android:id="@+id/test_2"
android:title="资讯"
android:icon="@mipmap/ic_launcher"
android:icon="@drawable/ic_t2"
></item>
<item
android:id="@+id/test_3"
android:title="我的"
android:icon="@mipmap/ic_launcher"
android:icon="@drawable/ic_t3"
></item>
</menu>
<item
android:id="@+id/test_4"
android:title="信息"
android:icon="@drawable/ic_t4"
></item>
</menu>
<!--shiftedColor="@color/colorPrimaryDark"-->
1 change: 1 addition & 0 deletions bottom-navigation-bar/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ dependencies {
})
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
compile 'org.jetbrains:annotations-java5:15.0'
}
71 changes: 71 additions & 0 deletions bottom-navigation-bar/src/main/java/com/wakehao/bar/BarUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.wakehao.bar;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import org.jetbrains.annotations.NotNull;

/**
* Created by WakeHao on 2017/1/10.
*/

public class BarUtils {
/**
* 根据手机的分辨率从dp的单位转成为px(像素)
*/
public static int dip2px(Context context, float dpValue){

final float scale=context.getResources().getDisplayMetrics().density;

return (int)(dpValue*scale+0.5f);

}

/**
* 根据手机的分辨率从px(像素)的单位转成为dp
*/
public static int px2dip(Context context,float pxValue) {

final float scale=context.getResources().getDisplayMetrics().density;

return (int)(pxValue/scale+0.5f);

}

public static float px2sp(@NotNull Context context, float pxValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale);
}

public static float sp2px(@NotNull Context context, float spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (spValue * fontScale);
}

public static Drawable changeDrawableColor(int drawableRes, int colorRes, Context context) {
//Convert drawable res to bitmap
final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), drawableRes);
final Bitmap resultBitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth() - 1, bitmap.getHeight() - 1);
final Paint p = new Paint();
final Canvas canvas = new Canvas(resultBitmap);
canvas.drawBitmap(resultBitmap, 0, 0, p);

//Create new drawable based on bitmap
final Drawable drawable = new BitmapDrawable(context.getResources(), resultBitmap);
drawable.setColorFilter(new
PorterDuffColorFilter(colorRes, PorterDuff.Mode.MULTIPLY));
return drawable;
}
public static int getDeviceWidth(Context context)
{
return context.getResources().getDisplayMetrics().widthPixels;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
package com.wakehao.bar;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import java.util.List;
Expand All @@ -12,7 +31,16 @@
* Created by WakeHao on 2017/1/5.
*/

public class BottomNavigationBar extends LinearLayout{
public class BottomNavigationBar extends FrameLayout{


private BottomNavigationBarContent mBottomNavigationBarContent;
private int itemBackGroundRes;
private int mSwitchMode;
private float maxRadius;
private float currentRadius=0f;
private Paint mPaint;
private View circleView;


public BottomNavigationBar(Context context) {
Expand All @@ -26,17 +54,167 @@ public BottomNavigationBar(Context context, AttributeSet attrs) {
public BottomNavigationBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.BottomNavigationBar,defStyleAttr,R.style.BottomNavigationView);

TypedArray typedArray=context.obtainStyledAttributes(
attrs,R.styleable.BottomNavigationBar,defStyleAttr,R.style.BottomNavigationView);


if(typedArray.hasValue(R.styleable.BottomNavigationBar_switchMode)){
mSwitchMode=typedArray.getInt(R.styleable.BottomNavigationBar_switchMode,0);
}

if(typedArray.hasValue(R.styleable.BottomNavigationBar_barElevation)){
setBackgroundColor(Color.WHITE);
ViewCompat.setElevation(
this,typedArray.getDimensionPixelSize(R.styleable.BottomNavigationBar_barElevation,0));
}



itemBackGroundRes = typedArray.getResourceId(R.styleable.BottomNavigationBar_itemBackground, 0);

if(typedArray.hasValue(R.styleable.BottomNavigationBar_menu)){
ItemParser parser=new ItemParser(context);
ItemParser parser=new ItemParser(context,getDefaultConfig());
parser.parser(typedArray.getResourceId(R.styleable.BottomNavigationBar_menu,0));
List<BottomNavigationItem> bottomNavigationItems = parser.getBottomNavigationItems();
mBottomNavigationBarContent=new BottomNavigationBarContent(context);
mBottomNavigationBarContent.setItems(parser.getBottomNavigationItems());
FrameLayout.LayoutParams layoutParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
addView(mBottomNavigationBarContent,layoutParams);
mBottomNavigationBarContent.finishInit(parser.getBottomNavigationItems());
}
else {

}
typedArray.recycle();
if(Build.VERSION.SDK_INT<21){
//TODO :addShade
View shadowView=new View(context);
shadowView.setBackgroundResource(R.drawable.shadow);
FrameLayout.LayoutParams layoutParams=new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, BarUtils.dip2px(context,1));
addView(shadowView,layoutParams);
}
else {
circleView = new View(context);
FrameLayout.LayoutParams layoutParams=new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
addView(circleView,layoutParams);
}

}

typedArray.recycle();
public void setFirstItemBackgroundColor(int shiftedColor){
setBackgroundColor(shiftedColor);
}
private BottomNavigationItem.Config getDefaultConfig() {
return new BottomNavigationItem.Config.Build()
//TODO setValue
.setItemBackGroundRes(itemBackGroundRes)
.setSwitchMode(mSwitchMode)
.build();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
widthMeasureSpec=MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec),MeasureSpec.EXACTLY);
heightMeasureSpec=MeasureSpec.makeMeasureSpec(BarUtils.dip2px(getContext(),56),MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

public void drawBackgroundCircle(int shiftedColor,float x,float y) {
if(mSwitchMode!=1)return;
if(currentRadius==0f)currentRadius=BarUtils.dip2px(getContext(),2);
if(maxRadius==0f)maxRadius = (float) Math.sqrt(getMeasuredHeight()*getMeasuredHeight()+getMeasuredWidth()*getMeasuredWidth());
if(Build.VERSION.SDK_INT<21){
drawLowVersionCircle(shiftedColor,x,y);
}
else {
prepareForBackgroundColorAnimation(shiftedColor);
drawHighVersionCircle(shiftedColor,(int)x,(int)y);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void drawHighVersionCircle(final int shiftedColor, int x, int y) {
Animator animator = ViewAnimationUtils.createCircularReveal(
circleView,
x,
y,
currentRadius,
maxRadius
);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
onEnd();
}

@Override
public void onAnimationCancel(Animator animation) {
onEnd();
}

private void onEnd() {
setBackgroundColor(shiftedColor);
circleView.setVisibility(View.INVISIBLE);
ViewCompat.setAlpha(circleView, 1);
}
});
animator.start();
}

private void prepareForBackgroundColorAnimation(int newColor) {
circleView.clearAnimation();

circleView.setBackgroundColor(newColor);
circleView.setVisibility(View.VISIBLE);
}
private float downX;
private float downY;
private void drawLowVersionCircle(int shiftedColor,float x,float y) {
downX=x;
downY=y;
this.shiftedColor=shiftedColor;
isStart=true;
invalidate();
}
private int shiftedColor;
private boolean isStart;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

if(isStart){
initPaint();
mPaint.setColor(shiftedColor);
if(currentRadius<maxRadius){
currentRadius+=currentRadius+maxRadius/30;
canvas.drawCircle(downX,downY,currentRadius,mPaint);
invalidate();
}
else{
isStart=false;
currentRadius=0f;
setBackgroundColor(shiftedColor);
canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint);
}
}
}

private void initPaint() {
if(mPaint==null){
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
}
}


public interface OnNavigationItemSelectedListener {

boolean onNavigationItemSelected(@NonNull BottomNavigationItem item,int selectedPosition);
}

public void setOnNavigationItemSelectedListener(
@Nullable OnNavigationItemSelectedListener listener) {
mBottomNavigationBarContent.injectListener(listener);
}

}
Loading

0 comments on commit 484052a

Please sign in to comment.