Skip to content

Commit

Permalink
ADD: rounded corners option + UPDATE: sample
Browse files Browse the repository at this point in the history
  • Loading branch information
vipulasri committed Nov 16, 2017
1 parent 439f64c commit ab5df29
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 21 deletions.
7 changes: 2 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
buildscript {
ext.versions = [
'compileSdk' : 27,
'buildTools' : '27.0.0',
'buildTools' : '27.0.1',
'minSdk' : 15,
'targetSdk' : 27,
'supportLibrary': '27.0.0',
'supportLibrary': '27.0.1',
'butterknife' : '8.8.1',
'kotlin' : '1.1.51'
]
Expand All @@ -21,9 +21,6 @@ buildscript {

classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

Expand Down
1 change: 1 addition & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
implementation 'com.dmitrymalkovich.android:material-design-dimens:1.4'
implementation 'org.adw.library:discrete-seekbar:1.0.1'
implementation project(":ticketview")
//implementation 'com.vipulasri:ticketview:1.0'
}
repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.adw.library.widgets.discreteseekbar.DiscreteSeekBar
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.CompoundButton
import kotlinx.android.synthetic.main.bottomsheet_ticket_attributes.*
import kotlinx.android.synthetic.main.item_corner_options.*


class MainActivity : AppCompatActivity() {
Expand Down Expand Up @@ -152,6 +153,29 @@ class MainActivity : AppCompatActivity() {
seekBar_divider_dash_gap.progress = Utils.pxToDp(ticketView.dividerDashGap.toFloat(), this)
seekBar_divider_dash_gap.setOnProgressChangeListener(progressChangeListener)

//corner properties

spinner_corner_type.setSelection(0)
spinner_corner_type.onItemSelectedListener = object : OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
val selectedItem = parent.getItemAtPosition(position).toString()
when (selectedItem) {
"Normal" -> ticketView.cornerType = TicketView.CornerType.NORMAL
"Rounded" -> ticketView.cornerType = TicketView.CornerType.ROUNDED
else -> {
ticketView.cornerType = TicketView.CornerType.NORMAL
}
}
}

override fun onNothingSelected(parent: AdapterView<*>) {

}
}

seekBar_corner_radius.progress = Utils.pxToDp(ticketView.cornerRadius.toFloat(), this)
seekBar_corner_radius.setOnProgressChangeListener(progressChangeListener)

}

private fun showColorPicker(selectedColor : Int, colorView: ImageView) {
Expand Down Expand Up @@ -182,13 +206,14 @@ class MainActivity : AppCompatActivity() {
override fun onProgressChanged(discreteSeekBar: DiscreteSeekBar, value: Int, b: Boolean) {

val valueInDp = Utils.dpToPx(value.toFloat(), this@MainActivity)
Log.e("TAG", "->"+discreteSeekBar.id)
Log.d("TAG", "->"+discreteSeekBar.id)
when(discreteSeekBar.id) {
R.id.seekBar_border_width -> ticketView.borderWidth = valueInDp
R.id.seekBar_scallop_radius -> ticketView.scallopRadius = valueInDp
R.id.seekBar_divider_width -> ticketView.dividerWidth = valueInDp
R.id.seekBar_divider_dash_length -> ticketView.dividerDashLength = valueInDp
R.id.seekBar_divider_dash_gap -> ticketView.dividerDashGap = valueInDp
R.id.seekBar_corner_radius -> ticketView.cornerRadius = valueInDp
}
}

Expand Down
6 changes: 6 additions & 0 deletions sample/src/main/res/layout/bottomsheet_ticket_attributes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@

<include layout="@layout/item_divider_options"/>

<android.support.v4.widget.Space
android:layout_width="match_parent"
android:layout_height="20dp" />

<include layout="@layout/item_corner_options"/>

</LinearLayout>

</android.support.v4.widget.NestedScrollView>
Expand Down
4 changes: 2 additions & 2 deletions sample/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
tools:showIn="@layout/activity_main">

<com.vipulasri.ticketview.TicketView
app:orientation="horizontal"
app:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_marginTop="60dp"
Expand All @@ -19,7 +19,7 @@
android:id="@+id/ticketView"
app:scallopRadius="10dp"
app:showBorder="false"
app:scallopPositionPercent="50"
app:scallopPositionPercent="30"
app:showDivider="true"
app:dividerType="dash"/>

Expand Down
54 changes: 54 additions & 0 deletions sample/src/main/res/layout/item_corner_options.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">

<android.support.v7.widget.AppCompatTextView
style="@style/Heading"
android:text="@string/corners" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center_vertical"
android:orientation="horizontal">

<android.support.v7.widget.AppCompatTextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="@string/type"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Caption"/>

<android.support.v7.widget.AppCompatSpinner
android:layout_width="140dp"
android:layout_height="wrap_content"
android:id="@+id/spinner_corner_type"
android:spinnerMode="dropdown"
android:entries="@array/corner_type"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center_vertical"
android:orientation="horizontal">

<android.support.v7.widget.AppCompatTextView
style="@style/Label"
android:text="@string/radius" />

<org.adw.library.widgets.discreteseekbar.DiscreteSeekBar
style="@style/DiscreteSeekBar"
android:id="@+id/seekBar_corner_radius"
app:dsb_min="0"
app:dsb_max="40"
app:dsb_value="10"/>

</LinearLayout>

</LinearLayout>
10 changes: 8 additions & 2 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
<string name="movie_title"><![CDATA[Fantastic Beast & Where to Find Them]]></string>
<string name="movie_duration">2h 13min</string>
<string name="movie_genre">Adventure, Fantasy, Action</string>
<string name="movie_ticket_number">400-2000-2020</string>
<string name="movie_ticket_number">400&#8211;2000&#8211;2020</string>
<string name="date">Date</string>
<string name="movie_date">1/3/2017</string>
<string name="movie_date">1&#8531;3&#8531;2017</string>
<string name="time">Time</string>
<string name="movie_time">23:30</string>
<string name="seat">Seat</string>
<string name="movie_seat">C5-C8</string>
<string name="corners">Corners</string>

<string-array name="divider_type">
<item>Normal</item>
Expand All @@ -48,4 +49,9 @@
<item>100</item>
</string-array>

<string-array name="corner_type">
<item>Normal</item>
<item>Rounded</item>
</string-array>

</resources>
2 changes: 1 addition & 1 deletion ticketview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ext {
groupId = 'com.vipulasri'
libraryName = 'TicketView'
artifact = 'ticketview'
libraryVersion = '1.0'
libraryVersion = '1.0.1'
libraryDescription = 'A custom view to implement TicketView in android.'

siteUrl = 'https://github.com/vipulasri/TicketView'
Expand Down
118 changes: 108 additions & 10 deletions ticketview/src/main/java/com/vipulasri/ticketview/TicketView.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntDef;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand All @@ -39,12 +37,20 @@ public class TicketView extends View {
int DASH = 1;
}

@Retention(RetentionPolicy.SOURCE)
@IntDef({ CornerType.NORMAL, CornerType.ROUNDED })
public @interface CornerType {
int NORMAL = 0;
int ROUNDED = 1;
}

private Paint mBackgroundPaint = new Paint();
private Paint mBorderPaint = new Paint();
private Paint mDividerPaint = new Paint();
private int mOrientation;
private Path mPath = new Path();
private RectF mRect = new RectF();
private RectF mRoundedCornerArc = new RectF();
private int mScallopHeight;
private float mScallopPosition;
private float mScallopPositionPercent;
Expand All @@ -59,6 +65,8 @@ public class TicketView extends View {
private int mDividerType;
private int mDividerWidth;
private int mDividerColor;
private int mCornerType;
private int mCornerRadius;

public TicketView(Context context) {
super(context);
Expand Down Expand Up @@ -86,25 +94,75 @@ protected void onDraw(Canvas canvas) {
mPath.reset();
if (mOrientation == Orientation.HORIZONTAL) {
offset = (float) (((top + bottom) / mScallopPosition) - mScallopRadius);
mPath.moveTo((float) left, (float) top);
mPath.lineTo((float) right, (float) top);

if(mCornerType == CornerType.ROUNDED) {
mPath.arcTo(getTopLeftCornerRoundedArc(top, left), 180.0f, 90.0f, false);
mPath.lineTo((float) left + mCornerRadius, (float) top);

mPath.lineTo((float) right - mCornerRadius, (float) top);
mPath.arcTo(getTopRightCornerRoundedArc(top, right), -90.0f, 90.0f, false);
} else {
mPath.lineTo((float) left, (float) top);
mPath.lineTo((float) right, (float) top);
}

mRect.set((float) (right - mScallopRadius), ((float) top) + offset, (float) (right + mScallopRadius), (((float) mScallopHeight) + offset) + ((float) top));
mPath.arcTo(mRect, 270, -180.0f, false);
mPath.lineTo((float) right, (float) bottom);
mPath.lineTo((float) left, (float) bottom);

if(mCornerType == CornerType.ROUNDED) {

mPath.arcTo(getBottomRightCornerRoundedArc(bottom, right), 0.0f, 90.0f, false);
mPath.lineTo((float) right - mCornerRadius, (float) bottom);

mPath.lineTo((float) left + mCornerRadius, (float) bottom);
mPath.arcTo(getBottomLeftCornerRoundedArc(left, bottom), 90.0f, 90.0f, false);

} else {
mPath.lineTo((float) right, (float) bottom);
mPath.lineTo((float) left, (float) bottom);
}

mRect.set((float) (left - mScallopRadius), ((float) top) + offset, (float) (left + mScallopRadius), (((float) mScallopHeight) + offset) + ((float) top));
mPath.arcTo(mRect, 90.0f, -180.0f, false);
mPath.close();
} else {
offset = (float) (((right + left) / mScallopPosition) - mScallopRadius);
mPath.moveTo((float) left, (float) top);

if(mCornerType == CornerType.ROUNDED) {
mPath.arcTo(getTopLeftCornerRoundedArc(top, left), 180.0f, 90.0f, false);
mPath.lineTo((float) left + mCornerRadius, (float) top);
} else {
mPath.lineTo((float) left, (float) top);
}

mRect.set(((float) left) + offset, (float) (top - mScallopRadius), (((float) mScallopHeight) + offset) + ((float) left), (float) (top + mScallopRadius));
mPath.arcTo(mRect, 180.0f, -180.0f, false);
mPath.lineTo((float) right, (float) top);
mPath.lineTo((float) right, (float) bottom);

if(mCornerType == CornerType.ROUNDED) {

mPath.lineTo((float) right - mCornerRadius, (float) top);
mPath.arcTo(getTopRightCornerRoundedArc(top, right), -90.0f, 90.0f, false);

mPath.arcTo(getBottomRightCornerRoundedArc(bottom, right), 0.0f, 90.0f, false);
mPath.lineTo((float) right - mCornerRadius, (float) bottom);

} else {
mPath.lineTo((float) right, (float) top);
mPath.lineTo((float) right, (float) bottom);
}

mRect.set(((float) left) + offset, (float) (bottom - mScallopRadius), (((float) mScallopHeight) + offset) + ((float) left), (float) (bottom + mScallopRadius));
mPath.arcTo(mRect, 0.0f, -180.0f, false);
mPath.lineTo((float) left, (float) bottom);

if(mCornerType == CornerType.ROUNDED) {

mPath.arcTo(getBottomLeftCornerRoundedArc(left, bottom), 90.0f, 90.0f, false);
mPath.lineTo((float) left, (float) bottom - mCornerRadius);

} else {
mPath.lineTo((float) left, (float) bottom);
}

mPath.close();
}

Expand Down Expand Up @@ -142,6 +200,8 @@ private void init(AttributeSet attrs) {
mDividerColor = typedArray.getColor(R.styleable.TicketView_dividerColor, getResources().getColor(android.R.color.darker_gray));
mDividerDashLength = typedArray.getDimensionPixelSize(R.styleable.TicketView_dividerDashLength, Utils.dpToPx(8f, getContext()));
mDividerDashGap = typedArray.getDimensionPixelSize(R.styleable.TicketView_dividerDashGap, Utils.dpToPx(4f, getContext()));
mCornerType = typedArray.getInt(R.styleable.TicketView_cornerType, CornerType.NORMAL);
mCornerRadius = typedArray.getDimensionPixelSize(R.styleable.TicketView_cornerRadius, Utils.dpToPx(4f, getContext()));

typedArray.recycle();
}
Expand Down Expand Up @@ -196,6 +256,26 @@ private void setDividerPaint() {

}

private RectF getTopLeftCornerRoundedArc(int top, int left){
mRoundedCornerArc.set((float) left, (float) top , (float) left + mCornerRadius * 2, (float) top + mCornerRadius * 2);
return mRoundedCornerArc;
}

private RectF getTopRightCornerRoundedArc(int top, int right){
mRoundedCornerArc.set(((float) right) - mCornerRadius * 2, (float) top, ((float) right), (float) (top + mCornerRadius * 2));
return mRoundedCornerArc;
}

private RectF getBottomLeftCornerRoundedArc(int left, int bottom){
mRoundedCornerArc.set(((float) left), (float) bottom - mCornerRadius * 2 , (float) left + mCornerRadius * 2, (float) bottom);
return mRoundedCornerArc;
}

private RectF getBottomRightCornerRoundedArc(int bottom, int right){
mRoundedCornerArc.set(((float) right) - mCornerRadius * 2, (float) bottom - mCornerRadius * 2 , ((float) right), (float) bottom);
return mRoundedCornerArc;
}

public int getOrientation() {
return mOrientation;
}
Expand Down Expand Up @@ -312,4 +392,22 @@ public void setDividerColor(int dividerColor) {
this.mDividerColor = dividerColor;
initElements();
}

public int getCornerType() {
return mCornerType;
}

public void setCornerType(int cornerType) {
this.mCornerType = cornerType;
initElements();
}

public int getCornerRadius() {
return mCornerRadius;
}

public void setCornerRadius(int cornerRadius) {
this.mCornerRadius = cornerRadius;
initElements();
}
}
Loading

0 comments on commit ab5df29

Please sign in to comment.