java - SeekBar 侦听器在片段中使应用程序崩溃
问题描述
我在片段中有一个搜索栏,当我运行应用程序时它崩溃了。片段是主要活动的一部分。这是代码:
public class PalleteFragment extends Fragment {
public TextView textView;
private CanvasView canvas;
public PalleteFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_pallete, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekBar);
textView = (TextView) view.findViewById(R.id.textView);
textView.setText("Pen size: " + seekBar.getProgress());
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
canvas.setStrokeWidth(seekBar.getProgress());
textView.setText("Pen size: " + seekBar.getProgress());
}
...
});
}
}
我需要写一些东西吗?
简而言之,这是 CanvasView 类:
public class CanvasView extends View implements View.OnTouchListener {
private Paint mPaint;
private Path mPath;
private int mDrawColor;
private int mBackgroundColor;
private Canvas mExtraCanvas;
private Bitmap mExtraBitmap;
private int strokeWidth;
private GestureDetector mGestureDetector;
private static final float TOUCH_TOLERANCE = 4;
private float mX, mY;
CanvasView(Context context) {
super(context);
setOnTouchListener(this);
strokeWidth = 10;
mBackgroundColor = ResourcesCompat.getColor(getResources(),
R.color.simpleWhite, null);
mDrawColor = ResourcesCompat.getColor(getResources(),
R.color.simpleBlack, null);
mPath = new Path();
mPaint = new Paint();
mPaint.setColor(mDrawColor);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setStyle(Paint.Style.STROKE); // default: FILL
mPaint.setStrokeJoin(Paint.Join.ROUND); // default: MITER
mPaint.setStrokeCap(Paint.Cap.ROUND); // default: BUTT
mPaint.setStrokeWidth(strokeWidth);
}
public CanvasView(Context context, AttributeSet attributeSet,GestureDetector mGestureDetector) {
super(context);
this.mGestureDetector = mGestureDetector;
strokeWidth = 10;
setOnTouchListener(this);
mBackgroundColor = ResourcesCompat.getColor(getResources(),
R.color.simpleWhite, null);
mDrawColor = ResourcesCompat.getColor(getResources(),
R.color.simpleBlack, null);
mPath = new Path();
mPaint = new Paint();
mPaint.setColor(mDrawColor);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setStyle(Paint.Style.STROKE); // default: FILL
mPaint.setStrokeJoin(Paint.Join.ROUND); // default: MITER
mPaint.setStrokeCap(Paint.Cap.ROUND); // default: BUTT
mPaint.setStrokeWidth(strokeWidth);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mExtraBitmap, 0, 0, null);
mPaint.setStrokeWidth(strokeWidth);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchStart(x, y);
updateColor(PreferenceManager.getDefaultSharedPreferences(getContext()).getInt("color",getResources().getColor(R.color.simpleBlack)));
setStrokeWidth(PreferenceManager.getDefaultSharedPreferences(getContext()).getInt("stroke", 0));
break;
case MotionEvent.ACTION_MOVE:
touchMove(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touchUp();
break;
default:
// Do nothing.
}
return true;
}
private void touchStart(float x, float y) {
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touchMove(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
mExtraCanvas.drawPath(mPath, mPaint);
}
}
private void touchUp() {
mPath.reset();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
mGestureDetector.onTouchEvent(event);
return false;
}
public void erase(){
mExtraCanvas.drawColor(ResourcesCompat.getColor(getResources(),
R.color.simpleWhite, null));
}
public void setStrokeWidth(int width) {
mPaint.setStrokeWidth(width);
}
}
这门课超长,所以我省略了这个问题不需要的其他方法。
这是片段中的 XML:
<?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:tools="http://schemas.android.com/tools"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<SeekBar
android:id="@+id/seekBar"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pen Size: " />
</LinearLayout>
这是应用程序的目录:
解决方案
在片段的 xml 文件中使用 CanvasView:
<com.lionsilva.newpaint.customviews.CanvasView
android:id="@+id/canvas_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
然后像任何其他视图一样初始化它:
private CanvasView canvas;
canvas = view.findViewById(R.id.canvas_view);
canvas.setStrokeWidth( seekBar.getProgress() );
推荐阅读
- android - 如何在导航控件片段中关闭导航 DrawerLayout onBackPressed
- .htaccess - 如何启用 gzip 压缩
- javascript - JS数组3深迷路
- reactjs - 如何轻松地将输入与流体属性以及 grid.column 中的图标对齐?
- python - 如何使用同一对象内的另一个项目引用 json 对象中的一个项目
- python-3.x - 如何选择某些标头之间的所有节点?
- tfs - 在 TFS 中使用 main 和 dev 分支
- python - 遍历行和应用转换的有效方法
- r - 使用 sapply 而不是 for 循环
- r - 一次从excel中读取基于单元格地址的多个值