Update detail scene
This commit is contained in:
1 parent
2599b032f0
commit
a26686c31b
34 files changed
+1383
-149
No files matched your search
@@ -72,6 +72,10 @@ public class GalleryDetailParser {
|
||||
public int previewPageCount;
|
||||
|
||||
private String sortOut(String str) {
|
||||
// Avoid null
|
||||
if (str == null) {
|
||||
str = "";
|
||||
}
|
||||
return Utils.unescapeXml(str).trim();
|
||||
}
|
||||
|
||||
@@ -137,7 +141,7 @@ public class GalleryDetailParser {
|
||||
+ ".+?"
|
||||
+ "<td id=\"grt3\"><span id=\"rating_count\">([\\d|,]+)</span></td>" // 18 ratedTimes
|
||||
+ "</tr>"
|
||||
+ "<tr><td[^<>]*>Average: ([^<>]+)</td>" // 19 rating "Average: x.xx" or "Not Yet Rated"
|
||||
+ "<tr><td[^<>]*>([^<>]+)</td>" // 19 rating "Average: x.xx" or "Not Yet Rated"
|
||||
+ ".+?"
|
||||
+ "<a id=\"favoritelink\"[^<>]*>(.+?)</a>", Pattern.DOTALL); // 20 isFavored "Favorite Gallery" for favorite
|
||||
m = p.matcher(body);
|
||||
|
||||
@@ -16,61 +16,236 @@
|
||||
|
||||
package com.hippo.ehviewer.ui.scene;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.view.Gravity;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextSwitcher;
|
||||
import android.widget.TextView;
|
||||
import android.widget.ViewSwitcher;
|
||||
|
||||
import com.hippo.conaco.Conaco;
|
||||
import com.hippo.effect.ViewTransition;
|
||||
import com.hippo.ehviewer.EhApplication;
|
||||
import com.hippo.ehviewer.EhImageKeyFactory;
|
||||
import com.hippo.ehviewer.R;
|
||||
import com.hippo.ehviewer.client.EhClient;
|
||||
import com.hippo.ehviewer.data.GalleryDetail;
|
||||
import com.hippo.ehviewer.data.GalleryInfo;
|
||||
import com.hippo.ehviewer.util.Config;
|
||||
import com.hippo.ehviewer.util.EhUtils;
|
||||
import com.hippo.ehviewer.widget.LoadImageView;
|
||||
import com.hippo.ehviewer.widget.RatingView;
|
||||
import com.hippo.rippleold.RippleSalon;
|
||||
import com.hippo.scene.Announcer;
|
||||
import com.hippo.scene.Scene;
|
||||
import com.hippo.util.Log;
|
||||
|
||||
public class GalleryDetailScene extends Scene {
|
||||
public class GalleryDetailScene extends Scene implements View.OnClickListener {
|
||||
|
||||
public static final String KEY_GALLERY_INFO = "gallery_info";
|
||||
|
||||
private LoadImageView mThumb;
|
||||
private TextView mTitle;
|
||||
private TextView mUploader;
|
||||
private ViewGroup mHeader;
|
||||
LoadImageView mThumb;
|
||||
TextView mTitle;
|
||||
TextView mUploader;
|
||||
TextView mCategory;
|
||||
|
||||
private ViewGroup mActionCard;
|
||||
private View mRead;
|
||||
private View mDownload;
|
||||
|
||||
private ViewGroup mContent;
|
||||
private TextView mLanguage;
|
||||
private TextView mPages;
|
||||
private TextView mSize;
|
||||
private TextView mPosted;
|
||||
private TextView mResize;
|
||||
private TextView mFavoredTimes;
|
||||
private TextSwitcher mRatingText;
|
||||
private RatingView mRating;
|
||||
private TextView mFavorite;
|
||||
private TextView mTorrent;
|
||||
private TextView mShare;
|
||||
private TextView mRate;
|
||||
|
||||
private View mProgressBar;
|
||||
|
||||
private ViewTransition mViewTransition;
|
||||
|
||||
private void handleAnnouncer(Announcer announcer) {
|
||||
GalleryInfo gi;
|
||||
|
||||
if (announcer == null) {
|
||||
Log.e("No announcer in GalleryDetailScene, finish itself");
|
||||
finish();
|
||||
return;
|
||||
} else if (null != (gi = announcer.getParcelableExtra(KEY_GALLERY_INFO))){
|
||||
Conaco conaco = EhApplication.getConaco(getStageActivity());
|
||||
conaco.load(mThumb, EhImageKeyFactory.getThumbKey(gi.gid), gi.thumb);
|
||||
mTitle.setText(gi.title);
|
||||
mUploader.setText(gi.uploader);
|
||||
mCategory.setText(EhUtils.getCategory(gi.category));
|
||||
mCategory.setTextColor(EhUtils.getCategoryColor(gi.category));
|
||||
|
||||
EhClient.getInstance().getGalleryDetail(Config.getEhSource(),
|
||||
EhClient.getDetailUrl(Config.getEhSource(), gi.gid, gi.token, 0),
|
||||
new EhDetailListener());
|
||||
}
|
||||
|
||||
Context context = getStageActivity();
|
||||
Resources resources = context.getResources();
|
||||
|
||||
mViewTransition.showView(1);
|
||||
|
||||
RippleSalon.addRipple(mRead, false);
|
||||
RippleSalon.addRipple(mDownload, false);
|
||||
|
||||
mRead.setOnClickListener(this);
|
||||
mDownload.setOnClickListener(this);
|
||||
|
||||
mRatingText.setFactory(new RatingTextViewFactory());
|
||||
|
||||
Drawable favoriteDrawable = resources.getDrawable(R.drawable.ic_heart_theme_primary);
|
||||
Drawable torrentDrawable = resources.getDrawable(R.drawable.ic_water_pump_theme_primary);
|
||||
Drawable shareDrawable = resources.getDrawable(R.drawable.ic_share_theme_primary);
|
||||
Drawable rateDrawable = resources.getDrawable(R.drawable.ic_thumbs_up_down_theme_primary);
|
||||
|
||||
int drawableSize = resources.getDimensionPixelOffset(R.dimen.detail_action_size);
|
||||
favoriteDrawable.setBounds(0, 0, drawableSize, drawableSize);
|
||||
torrentDrawable.setBounds(0, 0, drawableSize, drawableSize);
|
||||
shareDrawable.setBounds(0, 0, drawableSize, drawableSize);
|
||||
rateDrawable.setBounds(0, 0, drawableSize, drawableSize);
|
||||
|
||||
mFavorite.setCompoundDrawables(null, favoriteDrawable, null, null);
|
||||
mTorrent.setCompoundDrawables(null, torrentDrawable, null, null);
|
||||
mShare.setCompoundDrawables(null, shareDrawable, null, null);
|
||||
mRate.setCompoundDrawables(null, rateDrawable, null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.scene_gallery_detail);
|
||||
|
||||
GalleryInfo gi = getAnnouncer().getParcelableExtra(KEY_GALLERY_INFO);
|
||||
mHeader = (ViewGroup) findViewById(R.id.header);
|
||||
mThumb = (LoadImageView) mHeader.findViewById(R.id.thumb);
|
||||
mTitle = (TextView) mHeader.findViewById(R.id.title);
|
||||
mUploader = (TextView) mHeader.findViewById(R.id.uploader);
|
||||
mCategory = (TextView) mHeader.findViewById(R.id.category);
|
||||
|
||||
mThumb = (LoadImageView) findViewById(R.id.thumb);
|
||||
mThumb.bringToFront();
|
||||
mActionCard = (ViewGroup) findViewById(R.id.action_card);
|
||||
mRead = mActionCard.findViewById(R.id.read);
|
||||
mDownload = mActionCard.findViewById(R.id.download);
|
||||
|
||||
Conaco conaco = EhApplication.getConaco(getStageActivity());
|
||||
conaco.load(mThumb, EhImageKeyFactory.getThumbKey(gi.gid), gi.thumb);
|
||||
mContent = (ViewGroup) findViewById(R.id.content);
|
||||
mLanguage = (TextView) mContent.findViewById(R.id.language);
|
||||
mPages = (TextView) mContent.findViewById(R.id.pages);
|
||||
mSize = (TextView) mContent.findViewById(R.id.size);
|
||||
mPosted = (TextView) mContent.findViewById(R.id.posted);
|
||||
mResize = (TextView) mContent.findViewById(R.id.resize);
|
||||
mFavoredTimes = (TextView) mContent.findViewById(R.id.favoredTimes);
|
||||
mRatingText = (TextSwitcher) mContent.findViewById(R.id.rating_text);
|
||||
mRating = (RatingView) mContent.findViewById(R.id.rating);
|
||||
mFavorite = (TextView) mContent.findViewById(R.id.favorite);
|
||||
mTorrent = (TextView) mContent.findViewById(R.id.torrent);
|
||||
mShare = (TextView) mContent.findViewById(R.id.share);
|
||||
mRate = (TextView) mContent.findViewById(R.id.rate);
|
||||
|
||||
mTitle = (TextView) findViewById(R.id.title);
|
||||
mTitle.setText(gi.title);
|
||||
mProgressBar = findViewById(R.id.progress_bar);
|
||||
|
||||
mUploader = (TextView) findViewById(R.id.uploader);
|
||||
mUploader.setText(gi.uploader);
|
||||
mViewTransition = new ViewTransition(mContent, mProgressBar);
|
||||
|
||||
handleAnnouncer(getAnnouncer());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (v == mRead) {
|
||||
|
||||
/*
|
||||
int source = Config.getEhSource();
|
||||
EhClient.getInstance().getGalleryDetail(source, EhClient.getDetailUrl(source, gi.gid, gi.token, 0), new EhClient.OnGetGalleryDetailListener() {
|
||||
@Override
|
||||
public void onSuccess(GalleryDetail galleryDetail) {
|
||||
Log.d(galleryDetail.torrentUrl);
|
||||
}
|
||||
} else if (v == mDownload) {
|
||||
|
||||
@Override
|
||||
public void onFailure(Exception e) {
|
||||
Log.d("", e);
|
||||
}
|
||||
});
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
private String getRatingText(float rating) {
|
||||
String undefine = "(´_ゝ`)";
|
||||
if (rating == Float.NaN) {
|
||||
return undefine;
|
||||
}
|
||||
|
||||
int resId = 0;
|
||||
switch (Math.round(rating * 2)) {
|
||||
case 0:
|
||||
resId = R.string.rating0; break;
|
||||
case 1:
|
||||
resId = R.string.rating1; break;
|
||||
case 2:
|
||||
resId = R.string.rating2; break;
|
||||
case 3:
|
||||
resId = R.string.rating3; break;
|
||||
case 4:
|
||||
resId = R.string.rating4; break;
|
||||
case 5:
|
||||
resId = R.string.rating5; break;
|
||||
case 6:
|
||||
resId = R.string.rating6; break;
|
||||
case 7:
|
||||
resId = R.string.rating7; break;
|
||||
case 8:
|
||||
resId = R.string.rating8; break;
|
||||
case 9:
|
||||
resId = R.string.rating9; break;
|
||||
case 10:
|
||||
resId = R.string.rating10; break;
|
||||
}
|
||||
|
||||
if (resId == 0) {
|
||||
return undefine;
|
||||
} else {
|
||||
return getStageActivity().getString(resId);
|
||||
}
|
||||
}
|
||||
|
||||
private String getAllRatingText(float rating, int ratedTimes) {
|
||||
return String.format(getStageActivity().getString(R.string.we_feel), getRatingText(rating)) +
|
||||
" (" + rating + ", " + ratedTimes + ")";
|
||||
}
|
||||
|
||||
private class RatingTextViewFactory implements ViewSwitcher.ViewFactory {
|
||||
@Override
|
||||
public View makeView() {
|
||||
TextView tv = new TextView(getStageActivity());
|
||||
tv.setGravity(Gravity.CENTER);
|
||||
return tv;
|
||||
}
|
||||
}
|
||||
|
||||
private class EhDetailListener extends EhClient.OnGetGalleryDetailListener {
|
||||
|
||||
@Override
|
||||
public void onSuccess(GalleryDetail galleryDetail) {
|
||||
Resources resources = getStageActivity().getResources();
|
||||
mLanguage.setText(galleryDetail.language);
|
||||
mPages.setText(String.format(resources.getString(R.string.page_count),
|
||||
galleryDetail.pageCount));
|
||||
mSize.setText(galleryDetail.size);
|
||||
mPosted.setText(galleryDetail.posted);
|
||||
mResize.setText(galleryDetail.resize);
|
||||
mFavoredTimes.setText(String.format(resources.getString(R.string.favorited_times),
|
||||
galleryDetail.favoredTimes));
|
||||
mRating.setRating(galleryDetail.rating);
|
||||
mRatingText.setText(getAllRatingText(galleryDetail.rating, galleryDetail.ratedTimes));
|
||||
mViewTransition.showView(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,7 +56,7 @@ import com.hippo.ehviewer.util.EhUtils;
|
||||
import com.hippo.ehviewer.widget.ContentLayout;
|
||||
import com.hippo.ehviewer.widget.LoadImageView;
|
||||
import com.hippo.ehviewer.widget.OffsetLayout;
|
||||
import com.hippo.ehviewer.widget.RatingView;
|
||||
import com.hippo.ehviewer.widget.SimpleRatingView;
|
||||
import com.hippo.ehviewer.widget.SearchBar;
|
||||
import com.hippo.ehviewer.widget.SearchDatabase;
|
||||
import com.hippo.ehviewer.widget.SearchLayout;
|
||||
@@ -757,25 +757,27 @@ public final class GalleryListScene extends Scene implements SearchBar.Helper,
|
||||
|
||||
@Override
|
||||
public View[] getFromViewSet(@NonNull Scene fromScene) {
|
||||
View[] views = new View[3];
|
||||
View[] views = new View[4];
|
||||
GalleryListScene scene = (GalleryListScene) fromScene;
|
||||
RecyclerView.ViewHolder viewHolder = scene.mContentRecyclerView.findViewHolderForAdapterPosition(mPosition);
|
||||
if (viewHolder != null) {
|
||||
GalleryHolder gh = (GalleryHolder) viewHolder;
|
||||
views[0] = gh.thumb;
|
||||
views[1] = gh.title;
|
||||
views[2] = gh.uploader;
|
||||
views[0] = gh.thumb;
|
||||
views[1] = gh.title;
|
||||
views[2] = gh.uploader;
|
||||
views[3] = gh.category;
|
||||
}
|
||||
return views;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View[] getToViewSet(@NonNull Scene toScene) {
|
||||
View[] views = new View[3];
|
||||
View sceneView = toScene.getSceneView();
|
||||
views[0] = sceneView.findViewById(R.id.thumb);
|
||||
views[1] = sceneView.findViewById(R.id.title);
|
||||
views[2] = sceneView.findViewById(R.id.uploader);
|
||||
View[] views = new View[4];
|
||||
GalleryDetailScene scene = (GalleryDetailScene) toScene;
|
||||
views[0] = scene.mThumb;
|
||||
views[1] = scene.mTitle;
|
||||
views[2] = scene.mUploader;
|
||||
views[3] = scene.mCategory;
|
||||
return views;
|
||||
}
|
||||
}
|
||||
@@ -820,7 +822,7 @@ public final class GalleryListScene extends Scene implements SearchBar.Helper,
|
||||
public LoadImageView thumb;
|
||||
public TextView title;
|
||||
public TextView uploader;
|
||||
public RatingView rating;
|
||||
public SimpleRatingView rating;
|
||||
public TextView category;
|
||||
public TextView posted;
|
||||
public TextView simpleLanguage;
|
||||
@@ -830,7 +832,7 @@ public final class GalleryListScene extends Scene implements SearchBar.Helper,
|
||||
thumb = (LoadImageView) itemView.findViewById(R.id.thumb);
|
||||
title = (TextView) itemView.findViewById(R.id.title);
|
||||
uploader = (TextView) itemView.findViewById(R.id.uploader);
|
||||
rating = (RatingView) itemView.findViewById(R.id.rating);
|
||||
rating = (SimpleRatingView) itemView.findViewById(R.id.rating);
|
||||
category = (TextView) itemView.findViewById(R.id.category);
|
||||
posted = (TextView) itemView.findViewById(R.id.posted);
|
||||
simpleLanguage = (TextView) itemView.findViewById(R.id.simple_language);
|
||||
|
||||
@@ -1,95 +1,111 @@
|
||||
/*
|
||||
* Copyright (C) 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
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* 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.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
|
||||
import com.hippo.ehviewer.R;
|
||||
import com.hippo.util.ViewUtils;
|
||||
|
||||
public class RatingView extends View {
|
||||
|
||||
private Drawable mStarDrawable;
|
||||
private Drawable mStarHalfDrawable;
|
||||
private int mRatingSize;
|
||||
private int mRatingInterval;
|
||||
private Drawable mStarOutlineDrawable;
|
||||
|
||||
private float mStarSize;
|
||||
private float mStarInterval;
|
||||
private int mStarCount;
|
||||
private boolean mIndicator;
|
||||
private float mMax;
|
||||
private float mRating;
|
||||
private int mRatingInt;
|
||||
|
||||
private int mShownStarCount;
|
||||
private int mShownStarHalfCount;
|
||||
private int mShownStarOutlineCount;
|
||||
|
||||
private OnRatingChangeListener mOnRatingChangeListener;
|
||||
|
||||
public RatingView(Context context) {
|
||||
super(context);
|
||||
init(context);
|
||||
init(context, null);
|
||||
}
|
||||
|
||||
public RatingView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init(context);
|
||||
init(context, attrs);
|
||||
}
|
||||
|
||||
public RatingView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
init(context);
|
||||
init(context, attrs);
|
||||
}
|
||||
|
||||
private void init(Context context) {
|
||||
Resources resources = context.getResources();
|
||||
mStarDrawable = resources.getDrawable(R.drawable.ic_star);
|
||||
mStarHalfDrawable = resources.getDrawable(R.drawable.ic_star_half);
|
||||
mRatingSize = resources.getDimensionPixelOffset(R.dimen.rating_size);
|
||||
mRatingInterval = resources.getDimensionPixelOffset(R.dimen.rating_interval);
|
||||
private void init(Context context, AttributeSet attrs) {
|
||||
mStarDrawable = context.getResources().getDrawable(R.drawable.ic_star_theme_accent);
|
||||
mStarHalfDrawable = context.getResources().getDrawable(R.drawable.ic_star_half_theme_accent);
|
||||
mStarOutlineDrawable = context.getResources().getDrawable(R.drawable.ic_star_outline_theme_accent);
|
||||
|
||||
mStarDrawable.setBounds(0, 0, mRatingSize, mRatingSize);
|
||||
mStarHalfDrawable.setBounds(0, 0, mRatingSize, mRatingSize);
|
||||
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RatingView);
|
||||
mStarSize = a.getDimension(R.styleable.RatingView_starSize, 10);
|
||||
mStarInterval = a.getDimension(R.styleable.RatingView_starInterval, 10);
|
||||
mStarCount = a.getInt(R.styleable.RatingView_starCount, 5);
|
||||
mIndicator = a.getBoolean(R.styleable.RatingView_isIndicator, true);
|
||||
mMax = a.getFloat(R.styleable.RatingView_max, 10f);
|
||||
mRating = a.getFloat(R.styleable.RatingView_rating, 0f);
|
||||
|
||||
a.recycle();
|
||||
|
||||
updateBounds();
|
||||
updateShownCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
setMeasuredDimension(mRatingSize * 5 + mRatingInterval * 4, mRatingSize);
|
||||
private void updateBounds() {
|
||||
int starSize = (int) mStarSize;
|
||||
mStarDrawable.setBounds(0, 0, starSize, starSize);
|
||||
mStarHalfDrawable.setBounds(0, 0, starSize, starSize);
|
||||
mStarOutlineDrawable.setBounds(0, 0, starSize, starSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
int ratingInt = mRatingInt;
|
||||
int step = mRatingSize + mRatingInterval;
|
||||
int numStar = ratingInt / 2;
|
||||
int numStarHalf = ratingInt % 2;
|
||||
int saved = canvas.save();
|
||||
while (numStar-- > 0) {
|
||||
mStarDrawable.draw(canvas);
|
||||
canvas.translate(step, 0);
|
||||
private void updateShownCount() {
|
||||
float step = mMax / mStarCount / 2;
|
||||
int shown = Math.round(mRating / step);
|
||||
if (shown >= mStarCount * 2) {
|
||||
mShownStarCount = mStarCount;
|
||||
mShownStarHalfCount = 0;
|
||||
mShownStarOutlineCount = 0;
|
||||
} else {
|
||||
mShownStarCount = shown / 2;
|
||||
mShownStarHalfCount = shown % 2;
|
||||
mShownStarOutlineCount = mStarCount - mShownStarCount - mShownStarHalfCount;
|
||||
}
|
||||
if (numStarHalf == 1) {
|
||||
mStarHalfDrawable.draw(canvas);
|
||||
}
|
||||
canvas.restoreToCount(saved);
|
||||
}
|
||||
|
||||
public void setOnRatingChangeListener(OnRatingChangeListener listener) {
|
||||
mOnRatingChangeListener = listener;
|
||||
}
|
||||
|
||||
public void setIndicator(boolean indicator) {
|
||||
mIndicator = indicator;
|
||||
}
|
||||
|
||||
public boolean isIndicator() {
|
||||
return mIndicator;
|
||||
}
|
||||
|
||||
public void setRating(float rating) {
|
||||
if (mRating != rating) {
|
||||
float oldRating = mRating;
|
||||
mRating = rating;
|
||||
int ratingInt = Math.round(rating * 2);
|
||||
if (mRatingInt != ratingInt) {
|
||||
mRatingInt = ratingInt;
|
||||
invalidate();
|
||||
updateShownCount();
|
||||
invalidate();
|
||||
if (mOnRatingChangeListener != null) {
|
||||
mOnRatingChangeListener.onRatingChange(oldRating, rating, false, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -97,4 +113,93 @@ public class RatingView extends View {
|
||||
public float getRating() {
|
||||
return mRating;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getSuggestedMinimumWidth() {
|
||||
return Math.max(super.getSuggestedMinimumWidth(),
|
||||
(int) ((mStarSize * mStarCount) + (mStarInterval * (mStarCount - 1))));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getSuggestedMinimumHeight() {
|
||||
return Math.max(super.getSuggestedMinimumHeight(), (int) mStarSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
setMeasuredDimension(ViewUtils.getSuitableSize(getSuggestedMinimumWidth(), widthMeasureSpec),
|
||||
ViewUtils.getSuitableSize(getSuggestedMinimumHeight(), heightMeasureSpec));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
int saved = canvas.save();
|
||||
|
||||
float moveStep = mStarSize + mStarInterval;
|
||||
int count = mShownStarCount;
|
||||
while (count-- > 0) {
|
||||
mStarDrawable.draw(canvas);
|
||||
canvas.translate(moveStep, 0);
|
||||
}
|
||||
count = mShownStarHalfCount;
|
||||
while (count-- > 0) {
|
||||
mStarHalfDrawable.draw(canvas);
|
||||
canvas.translate(moveStep, 0);
|
||||
}
|
||||
count = mShownStarOutlineCount;
|
||||
while (count-- > 0) {
|
||||
mStarOutlineDrawable.draw(canvas);
|
||||
canvas.translate(moveStep, 0);
|
||||
}
|
||||
|
||||
canvas.restoreToCount(saved);
|
||||
}
|
||||
|
||||
private float getRatingByX(float x) {
|
||||
float step = mStarSize + mStarInterval;
|
||||
float sum = x + mStarInterval;
|
||||
float ratingStep = mMax / mStarCount / 2;
|
||||
int count = (int) (sum / step) * 2;
|
||||
float remain = sum - (count / 2 * step);
|
||||
if (remain > mStarInterval + (mStarSize / 2)) {
|
||||
count += 2;
|
||||
} else if (remain > mStarInterval) {
|
||||
count += 1;
|
||||
}
|
||||
count = Math.min(mStarCount * 2, count);
|
||||
return ratingStep * count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTouchEvent(@NonNull MotionEvent event) {
|
||||
if (!mIndicator) {
|
||||
switch (event.getAction()) {
|
||||
case MotionEvent.ACTION_DOWN:
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
float rating = getRatingByX(event.getX());
|
||||
if (mRating != rating) {
|
||||
float oldRating = mRating;
|
||||
mRating = rating;
|
||||
updateShownCount();
|
||||
invalidate();
|
||||
if (mOnRatingChangeListener != null) {
|
||||
mOnRatingChangeListener.onRatingChange(oldRating, rating, true, false);
|
||||
}
|
||||
}
|
||||
case MotionEvent.ACTION_UP:
|
||||
case MotionEvent.ACTION_CANCEL:
|
||||
if (mOnRatingChangeListener != null) {
|
||||
mOnRatingChangeListener.onRatingChange(mRating, mRating, true, true);
|
||||
}
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public interface OnRatingChangeListener {
|
||||
void onRatingChange(float oldRating, float newRating, boolean byUser, boolean confirm);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright (C) 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
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* 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.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
|
||||
import com.hippo.ehviewer.R;
|
||||
|
||||
public class SimpleRatingView extends View {
|
||||
|
||||
private Drawable mStarDrawable;
|
||||
private Drawable mStarHalfDrawable;
|
||||
private Drawable mStarOutlineDrawable;
|
||||
private int mRatingSize;
|
||||
private int mRatingInterval;
|
||||
|
||||
private float mRating;
|
||||
private int mRatingInt;
|
||||
|
||||
public SimpleRatingView(Context context) {
|
||||
super(context);
|
||||
init(context);
|
||||
}
|
||||
|
||||
public SimpleRatingView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init(context);
|
||||
}
|
||||
|
||||
public SimpleRatingView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
init(context);
|
||||
}
|
||||
|
||||
private void init(Context context) {
|
||||
Resources resources = context.getResources();
|
||||
mStarDrawable = resources.getDrawable(R.drawable.ic_star);
|
||||
mStarHalfDrawable = resources.getDrawable(R.drawable.ic_star_half);
|
||||
mStarOutlineDrawable = resources.getDrawable(R.drawable.ic_star_outline);
|
||||
mRatingSize = resources.getDimensionPixelOffset(R.dimen.rating_size);
|
||||
mRatingInterval = resources.getDimensionPixelOffset(R.dimen.rating_interval);
|
||||
|
||||
mStarDrawable.setBounds(0, 0, mRatingSize, mRatingSize);
|
||||
mStarHalfDrawable.setBounds(0, 0, mRatingSize, mRatingSize);
|
||||
mStarOutlineDrawable.setBounds(0, 0, mRatingSize, mRatingSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
setMeasuredDimension(mRatingSize * 5 + mRatingInterval * 4, mRatingSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
int ratingInt = mRatingInt;
|
||||
int step = mRatingSize + mRatingInterval;
|
||||
int numStar = ratingInt / 2;
|
||||
int numStarHalf = ratingInt % 2;
|
||||
int saved = canvas.save();
|
||||
while (numStar-- > 0) {
|
||||
mStarDrawable.draw(canvas);
|
||||
canvas.translate(step, 0);
|
||||
}
|
||||
if (numStarHalf == 1) {
|
||||
mStarHalfDrawable.draw(canvas);
|
||||
canvas.translate(step, 0);
|
||||
}
|
||||
int numOutline = 5 - numStar - numStarHalf;
|
||||
while (numOutline-- > 0) {
|
||||
mStarOutlineDrawable.draw(canvas);
|
||||
canvas.translate(step, 0);
|
||||
}
|
||||
canvas.restoreToCount(saved);
|
||||
}
|
||||
|
||||
public void setRating(float rating) {
|
||||
if (mRating != rating) {
|
||||
mRating = rating;
|
||||
int ratingInt = Math.round(rating * 2);
|
||||
if (mRatingInt != ratingInt) {
|
||||
mRatingInt = ratingInt;
|
||||
invalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public float getRating() {
|
||||
return mRating;
|
||||
}
|
||||
}
|
||||
@@ -274,21 +274,23 @@ public final class Utils {
|
||||
}
|
||||
|
||||
private static final String[] ESCAPE_CHARATER_LIST = {
|
||||
"&",
|
||||
"<",
|
||||
">",
|
||||
""",
|
||||
"'",
|
||||
"×"
|
||||
"&",
|
||||
"<",
|
||||
">",
|
||||
""",
|
||||
"'",
|
||||
"×",
|
||||
" "
|
||||
};
|
||||
|
||||
private static final String[] UNESCAPE_CHARATER_LIST = {
|
||||
"&",
|
||||
"<",
|
||||
">",
|
||||
"\"",
|
||||
"'",
|
||||
"×"
|
||||
"&",
|
||||
"<",
|
||||
">",
|
||||
"\"",
|
||||
"'",
|
||||
"×",
|
||||
" "
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
package com.hippo.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.Button;
|
||||
|
||||
import com.hippo.cardsalon.CardHelper;
|
||||
import com.hippo.cardsalon.CardViewDelegate;
|
||||
|
||||
public class CardButton extends Button implements CardViewDelegate {
|
||||
|
||||
private CardHelper mCardHelper;
|
||||
|
||||
public CardButton(Context context) {
|
||||
super(context);
|
||||
init(context, null);
|
||||
}
|
||||
|
||||
public CardButton(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init(context, attrs);
|
||||
}
|
||||
|
||||
public CardButton(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
init(context, attrs);
|
||||
}
|
||||
|
||||
private void init(Context context, AttributeSet attrs) {
|
||||
mCardHelper = new CardHelper(this);
|
||||
mCardHelper.initialize(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBackgroundResource(int resid) {
|
||||
// Empty
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public void setBackgroundDrawable(Drawable drawable) {
|
||||
// Empty
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRawPadding(int left, int top, int right, int bottom) {
|
||||
super.setPadding(left, top, right, bottom);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public void setRawBackgroundDrawable(Drawable drawable) {
|
||||
super.setBackgroundDrawable(drawable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCardRadius(float radius) {
|
||||
mCardHelper.setCardRadius(radius);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getCardRadius() {
|
||||
return mCardHelper.getCardRadius();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCardBackgroundColor(int color) {
|
||||
mCardHelper.setCardBackgroundColor(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCardBackgroundColor() {
|
||||
return mCardHelper.getCardBackgroundColor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCardBoundSize(float size) {
|
||||
mCardHelper.setCardBoundSize(size);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getCardBoundSize() {
|
||||
return mCardHelper.getCardBoundSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCardBoundColor(int color) {
|
||||
mCardHelper.setCardBoundColor(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCardBoundColor() {
|
||||
return mCardHelper.getCardBoundColor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCardElevation(float elevation) {
|
||||
mCardHelper.setCardElevation(elevation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getCardElevation() {
|
||||
return mCardHelper.getCardElevation();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package com.hippo.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
import com.hippo.cardsalon.CardHelper;
|
||||
import com.hippo.cardsalon.CardViewDelegate;
|
||||
|
||||
public class CardLinearLayout extends LinearLayout implements CardViewDelegate {
|
||||
|
||||
private CardHelper mCardHelper;
|
||||
|
||||
public CardLinearLayout(Context context) {
|
||||
super(context);
|
||||
init(context, null);
|
||||
}
|
||||
|
||||
public CardLinearLayout(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init(context, attrs);
|
||||
}
|
||||
|
||||
public CardLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
init(context, attrs);
|
||||
}
|
||||
|
||||
private void init(Context context, AttributeSet attrs) {
|
||||
mCardHelper = new CardHelper(this);
|
||||
mCardHelper.initialize(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBackgroundResource(int resid) {
|
||||
// Empty
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public void setBackgroundDrawable(Drawable drawable) {
|
||||
// Empty
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPadding(int left, int top, int right, int bottom) {
|
||||
mCardHelper.setPadding(left, top, right, bottom);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRawPadding(int left, int top, int right, int bottom) {
|
||||
super.setPadding(left, top, right, bottom);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public void setRawBackgroundDrawable(Drawable drawable) {
|
||||
super.setBackgroundDrawable(drawable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCardRadius(float radius) {
|
||||
mCardHelper.setCardRadius(radius);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getCardRadius() {
|
||||
return mCardHelper.getCardRadius();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCardBackgroundColor(int color) {
|
||||
mCardHelper.setCardBackgroundColor(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCardBackgroundColor() {
|
||||
return mCardHelper.getCardBackgroundColor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCardBoundSize(float size) {
|
||||
mCardHelper.setCardBoundSize(size);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getCardBoundSize() {
|
||||
return mCardHelper.getCardBoundSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCardBoundColor(int color) {
|
||||
mCardHelper.setCardBoundColor(color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCardBoundColor() {
|
||||
return mCardHelper.getCardBoundColor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCardElevation(float elevation) {
|
||||
mCardHelper.setCardElevation(elevation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getCardElevation() {
|
||||
return mCardHelper.getCardElevation();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user