如何为 android 创建双重自定义视图?
How to create doubled custom view for android?
我决定尝试创建自定义视图。有必要在两个图层(顶部、底部)上创建具有某些功能(旋转、缩放、变换、添加叠加)的编辑器,并且可以移动分隔符以增加一个布局以方便图像编辑。有没有类似的作品,或者有人已经想好如何下手了?
我已经制作了一个自定义布局来满足您的要求。
以编程方式使用它,如示例
public class BicontLayout extends LinearLayout {
private View divider;
private View vTop, vBottom;
private LinearLayout.LayoutParams vTopParams, vBottomParams;
private int dividerClickableHeight;
public BicontLayout(Context ctx, View viewTop, View viewBottom) {
super(ctx);
this.vTop = viewTop;
this.vBottom = viewBottom;
setOrientation(LinearLayout.VERTICAL);
setWeightSum(1f);
vTopParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 0.5f);
vBottomParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 0.5f);
addView(vTop, vTopParams);
divider = new View(ctx);
divider.setBackgroundColor(Color.RED);
dividerClickableHeight = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50f, getResources().getDisplayMetrics());
addView(divider, LinearLayout.LayoutParams.MATCH_PARENT, 3);
addView(vBottom, vBottomParams);
}
private float yStartTouch;
private float yStartWeight;
private boolean isDragging;
private int[] dividerLocation = new int[2];
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if(onTouchEvent(ev)) return true;
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent me) {
switch(me.getAction()) {
case MotionEvent.ACTION_DOWN:
yStartTouch = me.getRawY();
yStartWeight = vTopParams.weight;
divider.getLocationOnScreen(dividerLocation);
isDragging = Math.abs(dividerLocation[1]-yStartTouch) < dividerClickableHeight/2;
break;
case MotionEvent.ACTION_MOVE:
if(!isDragging) break;
float yDelta = me.getRawY() - yStartTouch;
float yDeltaProg = yDelta/BicontLayout.this.getHeight();
float yNewProg = yStartWeight + yDeltaProg;
if(yNewProg<0.1f) yNewProg=0.1f;
if(yNewProg>0.9f) yNewProg=0.9f;
vTopParams.weight = yNewProg;
vTop.setLayoutParams(vTopParams);
vBottomParams.weight = 1f - yNewProg;
vBottom.setLayoutParams(vBottomParams);
break;
case MotionEvent.ACTION_UP:
isDragging=false;
break;
}
if(isDragging) return true;
return super.onTouchEvent(me);
}
} // author Tiziano Munegato
用法示例
// vTop : top view
// vBottom : bottom view
BicontLayout doubleLayout = new BicontLayout(getContext(), vTop, vBottom);
setContentView(doubleLayout);
编码愉快!
我决定尝试创建自定义视图。有必要在两个图层(顶部、底部)上创建具有某些功能(旋转、缩放、变换、添加叠加)的编辑器,并且可以移动分隔符以增加一个布局以方便图像编辑。有没有类似的作品,或者有人已经想好如何下手了?
我已经制作了一个自定义布局来满足您的要求。
以编程方式使用它,如示例
public class BicontLayout extends LinearLayout {
private View divider;
private View vTop, vBottom;
private LinearLayout.LayoutParams vTopParams, vBottomParams;
private int dividerClickableHeight;
public BicontLayout(Context ctx, View viewTop, View viewBottom) {
super(ctx);
this.vTop = viewTop;
this.vBottom = viewBottom;
setOrientation(LinearLayout.VERTICAL);
setWeightSum(1f);
vTopParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 0.5f);
vBottomParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 0.5f);
addView(vTop, vTopParams);
divider = new View(ctx);
divider.setBackgroundColor(Color.RED);
dividerClickableHeight = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50f, getResources().getDisplayMetrics());
addView(divider, LinearLayout.LayoutParams.MATCH_PARENT, 3);
addView(vBottom, vBottomParams);
}
private float yStartTouch;
private float yStartWeight;
private boolean isDragging;
private int[] dividerLocation = new int[2];
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if(onTouchEvent(ev)) return true;
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent me) {
switch(me.getAction()) {
case MotionEvent.ACTION_DOWN:
yStartTouch = me.getRawY();
yStartWeight = vTopParams.weight;
divider.getLocationOnScreen(dividerLocation);
isDragging = Math.abs(dividerLocation[1]-yStartTouch) < dividerClickableHeight/2;
break;
case MotionEvent.ACTION_MOVE:
if(!isDragging) break;
float yDelta = me.getRawY() - yStartTouch;
float yDeltaProg = yDelta/BicontLayout.this.getHeight();
float yNewProg = yStartWeight + yDeltaProg;
if(yNewProg<0.1f) yNewProg=0.1f;
if(yNewProg>0.9f) yNewProg=0.9f;
vTopParams.weight = yNewProg;
vTop.setLayoutParams(vTopParams);
vBottomParams.weight = 1f - yNewProg;
vBottom.setLayoutParams(vBottomParams);
break;
case MotionEvent.ACTION_UP:
isDragging=false;
break;
}
if(isDragging) return true;
return super.onTouchEvent(me);
}
} // author Tiziano Munegato
用法示例
// vTop : top view
// vBottom : bottom view
BicontLayout doubleLayout = new BicontLayout(getContext(), vTop, vBottom);
setContentView(doubleLayout);
编码愉快!