Add source

This commit is contained in:
seven332 committed 2015-01-27 20:10:48 +08:00
1 parent 43d71e8813
commit f09b2ec1fa
11 files changed
+355

No files matched your search

+1
View File
@@ -20,6 +20,7 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:name="com.hippo.ehviewer.EhApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2014-2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.ehviewer;
import android.app.Application;
import com.hippo.util.UiUtils;
public class EhApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
UiUtils.init(this);
}
}
@@ -0,0 +1,90 @@
/*
* Copyright (C) 2014-2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.util;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
public class UiUtils {
private static float sDensity;
private static float sScaledDensity;
/**
* Init UiUtils. Do is in Application.onCreate()
*
* @param context
*/
public static void init(Context context) {
Resources resources = context.getResources();
sDensity = resources.getDisplayMetrics().density;
sScaledDensity = resources.getDisplayMetrics().scaledDensity;
}
/**
* dp conversion to pix
*
* @param dp The value you want to conversion
* @return value in pix
*/
public static int dp2pix(float dp) {
return (int) (sDensity * dp + 0.5f);
}
/**
* pix conversion to dp
*
* @param pix The value you want to conversion
* @return value in dp
*/
public static float pix2dp(int pix) {
return pix / sDensity;
}
/**
* sp conversion to pix
*
* @param sp The value you want to conversion
* @return value in pix
*/
public static int sp2pix(float sp) {
return (int) (sp * sScaledDensity + 0.5f);
}
/**
* pix conversion to sp
*
* @param pix The value you want to conversion
* @return value in sp
*/
public static float pix2sp(float pix) {
return pix / sScaledDensity;
}
/**
* get darker color of target color, alpha not change
* @param color the color you want to make it darker
* @return new darker color
*/
public static int getDarkerColor(int color) {
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= 0.9f;
return Color.HSVToColor(hsv);
}
}
@@ -245,4 +245,31 @@ public final class ViewUtils {
return result | (childMeasuredState&MEASURED_STATE_MASK);
}
/**
* Utility to return a default size. Uses the supplied size if the
* MeasureSpec imposed no constraints. Will get suitable if allowed
* by the MeasureSpec.
*
* @param size Default size for this view
* @param measureSpec Constraints imposed by the parent
* @return The size this view should be.
*/
public static int getSuitableSize(int size, int measureSpec) {
int result = size;
int specMode = View.MeasureSpec.getMode(measureSpec);
int specSize = View.MeasureSpec.getSize(measureSpec);
switch (specMode) {
case View.MeasureSpec.UNSPECIFIED:
result = size;
break;
case View.MeasureSpec.EXACTLY:
result = specSize;
break;
case View.MeasureSpec.AT_MOST:
result = size == 0 ? specSize : size;
}
return result;
}
}
@@ -0,0 +1,196 @@
/*
* Copyright (C) 2014-2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.widget;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import com.hippo.util.UiUtils;
import com.hippo.util.ViewUtils;
public class FloatingActionButton extends View {
private static final int[] BACKGROUND_ATTR = {android.R.attr.colorBackground};
private static final int[] DRAWABLE_ATTR = {android.R.attr.drawable};
private static final int DRAWABLE_WIDTH = UiUtils.dp2pix(24);
private GasketDrawer mGasketDrawer;
private Drawable mDrawable;
public FloatingActionButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0, 0);
}
public FloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr, 0);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public FloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs, defStyleAttr, defStyleRes);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
TypedArray a;
// Background color
a = context.obtainStyledAttributes(attrs, BACKGROUND_ATTR, defStyleAttr, defStyleRes);
int bgColor = a.getColor(0, Color.BLACK);
a.recycle();
// Drawable
a = context.obtainStyledAttributes(attrs, DRAWABLE_ATTR, defStyleAttr, defStyleRes);
mDrawable = a.getDrawable(0);
a.recycle();
mGasketDrawer = new GasketDrawer(this, bgColor);
}
protected void drawableStateChanged() {
int[] stateSet = getDrawableState();
mGasketDrawer.onStateChange(stateSet);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(ViewUtils.getSuitableSize(getSuggestedMinimumWidth(), widthMeasureSpec),
ViewUtils.getSuitableSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
protected void onDraw(Canvas canvas) {
mGasketDrawer.draw(canvas);
if (mDrawable != null) {
int left;
int top;
int right;
int bottom;
int width = getWidth();
int height = getHeight();
int halfDrawableWidth = DRAWABLE_WIDTH / 2;
left = width / 2 - halfDrawableWidth;
top = height / 2 - halfDrawableWidth;
right = left + DRAWABLE_WIDTH;
bottom = top + DRAWABLE_WIDTH;
mDrawable.setBounds(left, top, right, bottom);
mDrawable.draw(canvas);
}
}
public void setColor(int color) {
mGasketDrawer.setColor(color);
}
public void setDrawable(Drawable drawable) {
mDrawable = drawable;
invalidate();
}
static class GasketDrawer {
private static final int SHADOW_RADIOUS = UiUtils.dp2pix(3);
private static final int SHADOW_OFFSET_Y = UiUtils.dp2pix(1);
private static final int GASKET_PADDING = SHADOW_RADIOUS + SHADOW_OFFSET_Y; // 4dp
private static final int SHADOW_COLOR = 0x8a000000;
private View mView;
private int mColor;
private int mDarkerColor;
private Paint mPaint;
private RectF mBounds;
private boolean mIsDark;
private GasketDrawer(View view, int color) {
mView = view;
mBounds = new RectF();
mColor = color;
mDarkerColor = UiUtils.getDarkerColor(color);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
mPaint.setColor(mColor);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setShadowLayer(SHADOW_RADIOUS, 0, SHADOW_OFFSET_Y, SHADOW_COLOR);
ViewUtils.removeHardwareAccelerationSupport(mView);
}
protected void onStateChange(int[] stateSet) {
boolean enabled = false;
boolean pressed = false;
boolean focused = false;
for (int state : stateSet) {
if (state == android.R.attr.state_enabled) {
enabled = true;
}
if (state == android.R.attr.state_focused) {
focused = true;
}
if (state == android.R.attr.state_pressed) {
pressed = true;
}
}
setDark(!enabled || pressed || focused);
}
private void setDark(boolean isDark) {
if (mIsDark != isDark) {
mIsDark = isDark;
updateColor();
}
}
private void updateColor() {
mPaint.setColor(mIsDark ? mDarkerColor : mColor);
invalidateSelf();
}
private void invalidateSelf() {
mView.invalidate();
}
public void draw(Canvas canvas) {
mBounds.left = GASKET_PADDING;
mBounds.top = GASKET_PADDING;
mBounds.right = mView.getWidth() - GASKET_PADDING;
mBounds.bottom = mView.getHeight() - GASKET_PADDING;
canvas.drawOval(mBounds, mPaint);
}
public void setColor(int color) {
mColor = color;
mDarkerColor = UiUtils.getDarkerColor(color);
updateColor();
}
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 272 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 211 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 294 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 386 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 494 B

+11
View File
@@ -15,4 +15,15 @@
-->
<resources>
<!-- FloatingActionButton -->
<style name="FloatingActionButton">
<item name="android:minWidth">60dp</item>
<item name="android:minHeight">60dp</item>
</style>
<style name="FloatingActionButton.Small">
<item name="android:minWidth">44dp</item>
<item name="android:minHeight">44dp</item>
</style>
</resources>