gpt4 book ai didi

android - 如何在 Activity 类中使用自定义类(扩展 View 类)来绘制路径而不更改 android 中主布局的其他部分

转载 作者:行者123 更新时间:2023-11-29 00:25:30 26 4
gpt4 key购买 nike

我有以下两个类:1. ImageGalleryDemoActivity(从图库中导入图片)2. DrawClass(在导入的图片上徒手绘制路径)

public class ImageGalleryDemoActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {

Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();

ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));



}


}
}

和下面的类在导入的图像上绘制路径。

public class DrawClass extends View implements OnTouchListener {

private Paint paint;
List<Point> points;
int DIST = 2;
boolean flgPathDraw = true;

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.id.imgView);

public DrawClass(Context c ) {
super(c);
setFocusable(true);
setFocusableInTouchMode(true);

paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
paint.setColor(Color.BLACK);

this.setOnTouchListener(this);
points = new ArrayList<Point>();
}
public DrawClass(Context context, AttributeSet attrs) {
super(context, attrs);
setFocusable(true);
setFocusableInTouchMode(true);

paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
paint.setColor(Color.BLACK);

this.setOnTouchListener(this);
points = new ArrayList<Point>();

}

public void onDraw(Canvas canvas)
{
canvas.drawBitmap(bitmap, 0, 0, null);

Path path = new Path();
boolean first = true;

for (int i = 0; i < points.size(); i += 2)
{
Point point = points.get(i);
if (first) {
first = false;
path.moveTo(point.x, point.y);
} else if (i < points.size() - 1) {
Point next = points.get(i + 1);
path.quadTo(point.x, point.y, next.x, next.y);
} else {
path.lineTo(point.x, point.y);
}
}
canvas.drawPath(path, paint);
}

public boolean onTouch(View view, MotionEvent event) {
// if(event.getAction() != MotionEvent.ACTION_DOWN)
// return super.onTouchEvent(event);
Point point = new Point();
point.x = (int) event.getX();
point.y = (int) event.getY();

if (flgPathDraw) {
points.add(point);
}

invalidate();
Log.e("Hi ==>", "Size: " + points.size());

return true;
}
public void fillinPartofPath()
{
Point point = new Point();
point.x = points.get(0).x;
point.y = points.get(0).y;

points.add(point);
invalidate();
}
public void resetView()
{
points.clear();
paint.setColor(Color.WHITE);
paint.setStyle(Style.STROKE);
flgPathDraw=true;
invalidate();
}
}

class Point {
public float dy;
public float dx;
float x, y;

@Override
public String toString() {
return x + ", " + y;
}
}

现在,问题是我可以使用 setContentView 更改整个主布局,但我想通过“加载图片”按钮保留原始布局并在图像上绘制路径。那么,如何在 Activity 类中使​​用 DrawClass 类来保持原来的布局呢?

最佳答案

制作框架布局或相对布局不打扰原View

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:CustomTexView="http://schemas.android.com/apk/res/com.example.testapp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright"
android:orientation="vertical" >

<CustomView
android:layout_width="match_parent"
android:layout_height="match_parent" >
</CustomView>

<RelativeLayout
android:id="@+id/youroriginalviewhere"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>

</RelativeLayout>

关于android - 如何在 Activity 类中使用自定义类(扩展 View 类)来绘制路径而不更改 android 中主布局的其他部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19945504/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com