Skip to content

Commit

Permalink
添加游标的逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyq003 committed Dec 9, 2019
1 parent 0f2e8be commit cbf5bd8
Show file tree
Hide file tree
Showing 11 changed files with 439 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
}
88 changes: 84 additions & 4 deletions BreadScaleView/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,88 @@
android:gravity="center_horizontal"
tools:context=".MainActivity">

<com.cyq.library.BreadScaleView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<com.cyq.library.BreadScaleView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<!-- <FrameLayout-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content">-->

<!-- <ScrollView-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="400dp">-->

<!-- <LinearLayout-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent"-->
<!-- android:orientation="vertical">-->

<!-- <TextView-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="100dp"-->
<!-- android:text="哈哈哈哈哈哈哈" />-->

<!-- <TextView-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="100dp"-->
<!-- android:text="哈哈哈哈哈哈哈" />-->

<!-- <TextView-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="100dp"-->
<!-- android:text="哈哈哈哈哈哈哈" />-->

<!-- <TextView-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="100dp"-->
<!-- android:text="哈哈哈哈哈哈哈" />-->

<!-- <TextView-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="100dp"-->
<!-- android:text="哈哈哈哈哈哈哈" />-->

<!-- <TextView-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="100dp"-->
<!-- android:text="哈哈哈哈哈哈哈" />-->

<!-- <TextView-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="100dp"-->
<!-- android:text="哈哈哈哈哈哈哈" />-->

<!-- <TextView-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="100dp"-->
<!-- android:text="哈哈哈哈哈哈哈" />-->

<!-- <TextView-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="100dp"-->
<!-- android:text="哈哈哈哈哈哈哈" />-->

<!-- <TextView-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="100dp"-->
<!-- android:text="哈哈哈哈哈哈哈" />-->

<!-- <TextView-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="100dp"-->
<!-- android:text="哈哈哈哈哈哈哈" />-->

<!-- </LinearLayout>-->

<!-- </ScrollView>-->

<!-- <ImageView-->
<!-- android:background="@android:color/black"-->
<!-- android:layout_width="20dp"-->
<!-- android:layout_height="20dp"-->
<!-- android:layout_gravity="center_vertical"-->
<!-- android:layout_marginLeft="50dp" />-->

<!-- </FrameLayout>-->
</FrameLayout>
234 changes: 78 additions & 156 deletions BreadScaleView/library/src/main/java/com/cyq/library/BreadScaleView.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,84 @@
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ScrollView;

import java.util.ArrayList;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
* Time: 2019-12-07 21:44
* Author: ChenYangQi
* Description:烤面包刻度选择器
* @author : ChenYangQi
* date : 2019/12/9 9:08
* desc :
*/
public class BreadScaleView extends ObservableScrollView implements ObservableScrollView.OnScrollListener {
private List<ScaleBean> mData = new ArrayList<>();
private LinearLayout container;
public class BreadScaleView extends FrameLayout {
private BreadScrollView mBreadScrollView;
private ImageView ivCursor;
private Context mContext;
private int itemHeight = 160;//item默认高度
private int displayCount = 9;//默认展示9个item
private int tvSize = 24;//刻度文字默认大小,单位:sp
private int ivWidth = 140;//面包图片宽高
private int lineWidth = 200;//中间横线宽度
private int lineHeight = 12;//中间横线的高度
private int lineMarginLeft = 100;//刻度文字marginLeft
private int lineMarginRight = 70;//面包图标marginRight
private int lightColor = Color.parseColor("#616161");//未选中刻度颜色
private int middleColor = Color.parseColor("#fafafa");//二级刻度颜色
private int heightColor = Color.parseColor("#ff6d00");//三级刻度颜色
/**
* item默认高度
*/
private int itemHeight = 160;
/**
* 默认展示9个item
*/
private int displayCount = 9;
/**
* 刻度文字默认大小,单位:sp
*/
private int tvSize = 24;
/**
* 面包图片宽高
*/
private int ivWidth = 140;
/**
* 中间横线宽度
*/
private int lineWidth = 200;
/**
* 中间横线的高度
*/
private int lineHeight = 12;
/**
* 刻度文字marginLeft
*/
private int lineMarginLeft = 100;
/**
* 面包图标marginRight
*/
private int lineMarginRight = 100;
/**
* 未选中刻度颜色
*/
private int lightColor = Color.parseColor("#616161");
/**
* 二级刻度颜色
*/
private int middleColor = Color.parseColor("#fafafa");
/**
* 三级刻度颜色
*/
private int heightColor = Color.parseColor("#ff6d00");

/**
* 三角形游标的宽高
*/
private int cursorWidth = 60;

public BreadScaleView(Context context) {
public BreadScaleView(@NonNull Context context) {
this(context, null);
}

public BreadScaleView(Context context, AttributeSet attrs) {
public BreadScaleView(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}

public BreadScaleView(Context context, AttributeSet attrs, int defStyleAttr) {
public BreadScaleView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
Expand All @@ -53,136 +89,22 @@ public BreadScaleView(Context context, AttributeSet attrs, int defStyleAttr) {
* 控件初始化
*/
private void init() {
this.setVerticalScrollBarEnabled(false);
for (int i = 0; i < 20; i++) {
ScaleBean bean = new ScaleBean();
bean.setScale(String.valueOf(i + 1));
bean.setType(ScaleType.MIDDLE);
mData.add(bean);
}

mContext = getContext();
container = new LinearLayout(mContext);
container.setOrientation(LinearLayout.VERTICAL);
for (ScaleBean bean : mData) {
container.addView(createItemView(bean.getScale(), bean.getType()));
}
this.addView(container);
setOnScrollListener(this);
}

/**
* 创建Item
*
* @param scaleValue 刻度上要展示的文字
* @param empty 展示左侧面包的图标类型
* @return 返回item的View
*/
private View createItemView(String scaleValue, ScaleType empty) {
LinearLayout itemView = new LinearLayout(mContext);
itemView.setOrientation(LinearLayout.HORIZONTAL);
itemView.setGravity(Gravity.CENTER_VERTICAL);
ViewGroup.LayoutParams itemLayoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, itemHeight);
itemView.setLayoutParams(itemLayoutParams);

//创建刻度文字
TextView tv = new TextView(mContext);
MarginLayoutParams tvLayoutParams =
new MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, itemHeight);
tv.setLayoutParams(tvLayoutParams);
tv.setSingleLine(true);
//TODO 需要根据不同的状态变更颜色
tv.setTextColor(lightColor);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, tvSize);
tv.setText(scaleValue);
tv.setGravity(Gravity.CENTER);

//创建面包图片
ImageView iv = new ImageView(mContext);
MarginLayoutParams ivLayoutParams = new MarginLayoutParams(ivWidth, ivWidth);
iv.setLayoutParams(ivLayoutParams);
//TODO 替换成面包图标
iv.setBackgroundResource(R.drawable.bread_type_height);

//创建中间的横线刻度
View lineView = new View(mContext);
MarginLayoutParams layoutParams = new MarginLayoutParams(lineWidth, lineHeight);
layoutParams.leftMargin = lineMarginLeft;
layoutParams.rightMargin = lineMarginRight;
lineView.setLayoutParams(layoutParams);
//TODO 需要根据不同的状态变更颜色
lineView.setBackgroundColor(lightColor);

itemView.addView(iv);
itemView.addView(lineView);
itemView.addView(tv);
return itemView;
}

/**
* 修改ScrollView的滑动速度
*
* @param velocityY
*/
@Override
public void fling(int velocityY) {
super.fling(velocityY / 2);
}

/**
* 设置控件高度为=item高度*item个数
*
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(widthMeasureSpec, itemHeight * displayCount);
}

/**
* 滑动状态监听
*
* @param view
* @param scrollState
*/
@Override
public void onScrollStateChanged(ObservableScrollView view, int scrollState) {
switch (scrollState) {
case SCROLL_STATE_IDLE:
//停止滑动
if (!autoScrollTag) {
return;
}
int topCount = scrollY / itemHeight;
int criticalY = scrollY % itemHeight;
Log.i("test", "topCount:" + topCount + "-----criticalY:" + criticalY);
if (criticalY > itemHeight / 2) {
//滑动到下一个位置
BreadScaleView.this.smoothScrollTo(0, (topCount + 1) * itemHeight);
} else {
//滑动到上一个位置
BreadScaleView.this.smoothScrollTo(0, topCount * itemHeight);
}
autoScrollTag = false;
break;
case SCROLL_STATE_TOUCH_SCROLL://按下
case SCROLL_STATE_FLING://开始滑动
autoScrollTag = true;
break;
default:
}
}

int scrollY = 0;//标记Y方向已滑动距离
boolean autoScrollTag = false;//用来标记是否是自动滚动

@Override
public void onScroll(ObservableScrollView view, boolean isTouchScroll, int x, int y, int oldX
, int oldY) {
// Log.i("test", "fling-----");
scrollY = y;
//滑动布局属性
mBreadScrollView = new BreadScrollView(getContext());
mBreadScrollView.setLayoutParams(new ScrollView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

//游标布局属性
ivCursor = new ImageView(mContext);
int marginLeft = ivWidth + lineMarginLeft + lineWidth;
int marginTop = (displayCount / 2) * itemHeight + ((itemHeight - cursorWidth) / 2);
MarginLayoutParams ivLayoutParams = new MarginLayoutParams(cursorWidth, cursorWidth);
ivLayoutParams.leftMargin = marginLeft;
ivLayoutParams.topMargin = marginTop;
ivCursor.setLayoutParams(ivLayoutParams);
ivCursor.setBackgroundResource(R.drawable.bread_icon_pointer);

addView(mBreadScrollView);
addView(ivCursor);
}
}
Loading

0 comments on commit cbf5bd8

Please sign in to comment.