Skip to content

Commit

Permalink
add event listener
Browse files Browse the repository at this point in the history
  • Loading branch information
zhpan committed May 26, 2018
1 parent 182b323 commit 84a6238
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 30 deletions.
25 changes: 17 additions & 8 deletions app/src/main/java/com/zhpan/ovallockview/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,27 @@
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
import com.zhpan.ovallockview.listener.OnLockOperateListener;
import com.zhpan.ovallockview.view.OvalLockView;

public class MainActivity extends AppCompatActivity {
private OvalLockView mOvalLockView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* OvalLockView ovalLockView= findViewById(R.id.olv);
ovalLockView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "hahaha", Toast.LENGTH_SHORT).show();
}
});*/
mOvalLockView=findViewById(R.id.lock_view);
mOvalLockView.setOnLockOperateListener(new OnLockOperateListener() {
@Override
public void onLockStart() {
Toast.makeText(MainActivity.this, "正在上锁", Toast.LENGTH_SHORT).show();

}

@Override
public void onUnlockStart() {
Toast.makeText(MainActivity.this, "正在开锁", Toast.LENGTH_SHORT).show();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.zhpan.ovallockview.listener;

public interface OnLockOperateListener {
void onLockStart();

void onUnlockStart();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.zhpan.ovallockview;
package com.zhpan.ovallockview.utils;

import android.content.Context;
import android.util.DisplayMetrics;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.zhpan.ovallockview;
package com.zhpan.ovallockview.view;

import android.content.Context;
import android.content.res.TypedArray;
Expand All @@ -10,14 +10,17 @@
import android.view.View;
import android.widget.Scroller;

import com.zhpan.ovallockview.utils.DensityUtils;
import com.zhpan.ovallockview.R;


/**
* Created by zhpan on 2017/5/31.
* Description: 自定义圆
*/

public class CircleView extends View {
private int background;
private int circleColor;
private int mTextColor;
private int mWidth;
private int mHeight;
Expand Down Expand Up @@ -45,9 +48,9 @@ public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
if (attrs != null) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleView);
background = typedArray.getColor(R.styleable.CircleView_circle_color, context.getResources().getColor(R.color.red));
mTextColor = typedArray.getColor(R.styleable.CircleView_text_color, context.getResources().getColor(R.color.red));
mTextSize = typedArray.getDimension(R.styleable.CircleView_text_size, 16.0f);
circleColor = typedArray.getColor(R.styleable.CircleView_circle_color, context.getResources().getColor(R.color.red));
mTextColor = typedArray.getColor(R.styleable.CircleView_text_color, context.getResources().getColor(R.color.white));
mTextSize = typedArray.getDimension(R.styleable.CircleView_text_size, DensityUtils.dp2px(context,16));
mText = typedArray.getString(R.styleable.CircleView_text_str);
typedArray.recycle();
}
Expand All @@ -58,7 +61,7 @@ private void init(Context context) {
mPaint = new Paint();
mPaint.setStyle(Paint.Style.FILL);
mPaint.setAntiAlias(true);
mPaint.setColor(background);
mPaint.setColor(circleColor);

mPaintText = new Paint();
mPaintText.setColor(mTextColor);
Expand Down Expand Up @@ -88,6 +91,7 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(circleColor);
canvas.drawCircle(mPieCenterX, mPieCenterY, mRadius, mPaint);
drawText(canvas);
}
Expand All @@ -112,6 +116,15 @@ public void setText(String text) {
invalidate();
}

public int getCircleColor() {
return circleColor;
}

public void setCircleColor(int background) {
this.circleColor = background;
invalidate();
}

public void smoothScroll(int destX, int destY) {
int scrollY = getScrollY();
int delta = destY - scrollY;
Expand All @@ -126,4 +139,12 @@ public void computeScroll() {
postInvalidate();
}
}

public Scroller getScroller() {
return mScroller;
}

public void setScroller(Scroller scroller) {
mScroller = scroller;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.zhpan.ovallockview;
package com.zhpan.ovallockview.view;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Scroller;

import com.zhpan.ovallockview.R;
import com.zhpan.ovallockview.listener.OnLockOperateListener;

public class OvalLockView extends FrameLayout {
private Handler mHandler = new Handler();
private CircleView mOvalView;
Expand All @@ -19,6 +20,15 @@ public class OvalLockView extends FrameLayout {
private int mTouchSlop;
private int mHeight;
private int mWidth;
private Context mContext;
private Option mOption;
private String text;
private OnLockOperateListener mOnLockOperateListener;
private int viewColor;
private enum Option{
LOCK,
UNLOCK
}
/* private Runnable mRunnable = new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -103,13 +113,41 @@ public boolean onTouchEvent(MotionEvent event) {
break;
case MotionEvent.ACTION_MOVE:
int deltaY = mLastY - y;
if (Math.abs(mOvalView.getScrollY()) > (mHeight / 2 - mOvalView.getWidth() / 2-mHeight/13)){
if(mOvalView.getScrollY()>0){
mOption=Option.LOCK;
}else {
mOption=Option.UNLOCK;
}
if (Math.abs(mOvalView.getScrollY()) > (mHeight / 2 - mOvalView.getWidth() / 2-mHeight/20)){
switch (mOption){
case LOCK:
mOvalView.setText("释放上锁");
mOvalView.setCircleColor(mContext.getResources().getColor(R.color.red));
break;
case UNLOCK:
mOvalView.setText("释放开锁");
mOvalView.setCircleColor(mContext.getResources().getColor(R.color.green));
break;
}
return true;
} else
mOvalView.scrollBy(0, deltaY);
break;
case MotionEvent.ACTION_UP:
switch (mOption){
case LOCK:
mOvalView.setText("正在上锁");
mOvalView .setCircleColor(getResources().getColor(R.color.red));
mOnLockOperateListener.onLockStart();
break;
case UNLOCK:
mOvalView.setText("正在开锁");
mOvalView.setCircleColor(getResources().getColor(R.color.green));
mOnLockOperateListener.onUnlockStart();
break;
}
mOvalView.smoothScroll(0,0);

break;
}
mLastY = y;
Expand All @@ -120,7 +158,9 @@ private void init(Context context, AttributeSet attrs) {
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
View view = View.inflate(context, R.layout.layout_oval_lock, this);
mOvalView = view.findViewById(R.id.oval_view);
mScroller = new Scroller(context);
mOvalView.setText("已上锁");
mScroller = mOvalView.getScroller();
mContext=context;
mOvalView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -136,5 +176,17 @@ public void onClick(View v) {
});*/
}

public void setText(String text) {
this.text = text;
mOvalView.setText(text);
}

public void setCircleColor(int viewColor) {
this.viewColor = viewColor;
mOvalView.setCircleColor(viewColor);
}

public void setOnLockOperateListener(OnLockOperateListener onLockOperateListener) {
mOnLockOperateListener = onLockOperateListener;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.zhpan.ovallockview;/**
package com.zhpan.ovallockview.view;/**
* Created by jun on 16-7-14.
*/

Expand All @@ -8,9 +8,11 @@
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.zhpan.ovallockview.utils.DensityUtils;
import com.zhpan.ovallockview.R;

/**
* @Author Jun Cheung (Email:[email protected])
* @Date 16-7-14
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">

<com.zhpan.ovallockview.OvalLockView
<com.zhpan.ovallockview.view.OvalLockView
android:id="@+id/lock_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/res/layout/layout_oval_lock.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">

<com.zhpan.ovallockview.CircleView
<com.zhpan.ovallockview.view.CircleView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginTop="80dp"
android:layout_marginTop="100dp"
app:circle_color="@color/green"
android:layout_gravity="center"/>

<com.zhpan.ovallockview.CircleView
<com.zhpan.ovallockview.view.CircleView
android:layout_width="15dp"
android:layout_height="15dp"
app:circle_color="@color/red"
android:layout_marginTop="-80dp"
android:layout_marginTop="-100dp"
android:layout_gravity="center"/>

<com.zhpan.ovallockview.CircleView
<com.zhpan.ovallockview.view.CircleView
android:id="@+id/oval_view"
android:layout_width="150dp"
android:layout_height="300dp"
android:layout_width="200dp"
android:layout_height="340dp"
android:layout_gravity="center"
android:padding="20dp"/>
</FrameLayout>
Binary file added image/ezgif-1-fe4ce3ebf3.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 84a6238

Please sign in to comment.