[android] Added custom attributes for RatingView to make posissble setting rating from xml

This commit is contained in:
Александр Зацепин 2017-09-15 16:54:32 +03:00 committed by Roman Kuznetsov
parent cd0d71cc72
commit eab684ef73
7 changed files with 58 additions and 32 deletions

View file

@ -2,13 +2,14 @@
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item
android:id="@android:id/mask"
android:drawable="@android:color/white" />
<item android:id="@android:id/mask">
<shape android:shape="oval">
<solid android:color="@android:color/white"/>
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="@color/black_4"/>
<size android:width="40dp" android:height="40dp"/>
</shape>
</item>
</ripple>

View file

@ -3,7 +3,4 @@
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/black_4"/>
<size
android:width="40dp"
android:height="40dp"/>
</shape>

View file

@ -3,7 +3,4 @@
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/white_4"/>
<size
android:width="40dp"
android:height="40dp"/>
</shape>

View file

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll__pp_ugc"
android:layout_width="match_parent"
@ -39,9 +40,13 @@
android:gravity="center_horizontal">
<com.mapswithme.maps.widget.RatingView
android:id="@+id/iv__horrible"
android:padding="@dimen/margin_half"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?ratingButtonBackground"/>
android:layout_height="@dimen/rating_view_height"
android:background="?ratingButtonBackground"
android:visibility="visible"
app:rating="horrible"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@ -61,9 +66,11 @@
android:gravity="center_horizontal">
<com.mapswithme.maps.widget.RatingView
android:id="@+id/iv__bad"
android:padding="@dimen/margin_half"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?ratingButtonBackground"/>
android:layout_height="@dimen/rating_view_height"
android:background="?ratingButtonBackground"
app:rating="bad"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@ -83,9 +90,11 @@
android:gravity="center_horizontal">
<com.mapswithme.maps.widget.RatingView
android:id="@+id/iv__normal"
android:padding="@dimen/margin_half"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?ratingButtonBackground"/>
android:layout_height="@dimen/rating_view_height"
android:background="?ratingButtonBackground"
app:rating="normal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@ -105,9 +114,11 @@
android:gravity="center_horizontal">
<com.mapswithme.maps.widget.RatingView
android:id="@+id/iv__good"
android:padding="@dimen/margin_half"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?ratingButtonBackground"/>
android:layout_height="@dimen/rating_view_height"
android:background="?ratingButtonBackground"
app:rating="good"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@ -125,9 +136,11 @@
android:gravity="center_horizontal">
<com.mapswithme.maps.widget.RatingView
android:id="@+id/iv__excellent"
android:padding="@dimen/margin_half"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?ratingButtonBackground"/>
android:layout_height="@dimen/rating_view_height"
android:background="?ratingButtonBackground"
app:rating="excellent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

View file

@ -26,6 +26,17 @@
<attr name="progress" format="integer"/>
</declare-styleable>
<declare-styleable name="RatingView">
<attr name="android:textSize"/>
<attr name="rating" format="enum">
<enum name="horrible" value="0"/>
<enum name="bad" value="1"/>
<enum name="normal" value="2"/>
<enum name="good" value="3"/>
<enum name="excellent" value="4"/>
</attr>
</declare-styleable>
<bool name="isTablet">false</bool>
<bool name="tabletLayout">false</bool>
</resources>
</resources>

View file

@ -7,11 +7,11 @@ import com.mapswithme.maps.R;
public enum Rating
{
EXCELLENT(R.drawable.ic_24px_rating_excellent, R.color.rating_excellent),
GOOD(R.drawable.ic_24px_rating_good, R.color.rating_good),
NORMAL(R.drawable.ic_24px_rating_normal, R.color.rating_normal),
HORRIBLE(R.drawable.ic_24px_rating_horrible, R.color.rating_horrible),
BAD(R.drawable.ic_24px_rating_bad, R.color.rating_bad),
HORRIBLE(R.drawable.ic_24px_rating_horrible, R.color.rating_horrible);
NORMAL(R.drawable.ic_24px_rating_normal, R.color.rating_normal),
GOOD(R.drawable.ic_24px_rating_good, R.color.rating_good),
EXCELLENT(R.drawable.ic_24px_rating_excellent, R.color.rating_excellent);
@DrawableRes
private final int mDrawableId;

View file

@ -17,6 +17,7 @@ import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import com.mapswithme.maps.R;
import com.mapswithme.maps.ugc.Rating;
public class RatingView extends View
@ -53,21 +54,27 @@ public class RatingView extends View
private void init(@Nullable AttributeSet attrs)
{
int[] set = { android.R.attr.textSize };
TypedArray a = getContext().obtainStyledAttributes(
attrs, set);
float textSize = a.getDimensionPixelSize(0, 0);
a.recycle();
attrs, R.styleable.RatingView);
float textSize = a.getDimensionPixelSize(R.styleable.RatingView_android_textSize, 0);
mTextPaint.setTextSize(textSize);
mTextPaint.setTypeface(Typeface.create("Roboto", Typeface.BOLD));
int rating = a.getInteger(R.styleable.RatingView_rating, 0);
a.recycle();
Rating r = Rating.values()[rating];
setRating(r, null);
}
public void setRating(Rating rating, @Nullable String value)
{
//TODO: remove this code when place_page_info.cpp is ready and use rating parameter.
mRatingValue = value;
Rating r = dummyMethod();
Rating r = rating;
if (r == null)
r = dummyMethod();
Resources res = getContext().getResources();
mRatingColor = res.getColor(r.getColorId());
mBackgroundPaint.setColor(mRatingColor);
@ -81,7 +88,7 @@ public class RatingView extends View
//TODO: remove this code when place_page_info.cpp is ready and use rating parameter.
private Rating dummyMethod()
{
if (TextUtils.isEmpty(mRatingValue))
if (TextUtils.isEmpty(mRatingValue) || "-".equals(mRatingValue))
return Rating.EXCELLENT;
float rating = Float.valueOf(mRatingValue);