Android studio 如何在JavaandroidStudio中添加背景图像,或者可以在xmls中使用背景图像
我是android开发新手,似乎无法将xml文件中的背景图像与java类配合使用。我只能让其中一个工作,据我所知,我只能使用setContentView一次,对吗?我想我最好的改变是使用java来显示图像?在Java中如何实现这一点?我正在尝试创建一个绘画应用程序,但希望使用图像,以便用户可以在该图像上书写。。。。下面是我的Java代码:有人能帮我创建一个DeleteCanvas方法吗 我已经编辑了我的代码,现在我的背景图像正在显示,但没有显示setContentView(pv),因为它仍然不允许我在图像上绘制 这是我的PaintView课程Android studio 如何在JavaandroidStudio中添加背景图像,或者可以在xmls中使用背景图像,android-studio,Android Studio,我是android开发新手,似乎无法将xml文件中的背景图像与java类配合使用。我只能让其中一个工作,据我所知,我只能使用setContentView一次,对吗?我想我最好的改变是使用java来显示图像?在Java中如何实现这一点?我正在尝试创建一个绘画应用程序,但希望使用图像,以便用户可以在该图像上书写。。。。下面是我的Java代码:有人能帮我创建一个DeleteCanvas方法吗 我已经编辑了我的代码,现在我的背景图像正在显示,但没有显示setContentView(pv),因为它仍然不允
package com.example.paint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class PaintView extends View {
LayoutParams params;
Path path = new Path();
Paint br = new Paint();
public PaintView(Context context) {
super(context);
br.setColor(Color.GREEN);
br.setStyle(Paint.Style.STROKE);
br.setStrokeJoin(Paint.Join.ROUND);
br.setStrokeWidth(10f);
params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(x, y);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(x, y);
break;
}
postInvalidate();
return false;
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, br);
}
}
This is my MainActivity class:
package com.example.paint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.example.paint.PaintView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PaintView pv = new PaintView(this);
setContentView(pv);
ConstraintLayout constraintLayout = new ConstraintLayout(this);
// Instantiate an ImageView and define its properties
ImageView i = new ImageView(this);
i.setImageResource(R.drawable.worksheetblank);
// i.setContentDescription(getResources().getString(R.string.worksheetblank));
// set the ImageView bounds to match the Drawable's dimensions
i.setAdjustViewBounds(true);
i.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
// Add the ImageView to the layout and set the layout as the content view.
constraintLayout.addView(i);
setContentView(constraintLayout);
}
}
</androidx.constraintlayout.widget.ConstraintLayout>ode here
I am also wondering if I can get that rectangle to appear as well with my java drawing class?
package com.example.paint;
导入android.content.Context;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.Paint;
导入android.graphics.Path;
导入android.graphics.PorterDuff;
导入android.graphics.PorterDuffXfermode;
导入android.graphics.Rect;
导入android.view.MotionEvent;
导入android.view.view;
导入android.widget.LinearLayout;
导入android.widget.LinearLayout.LayoutParams;
公共类PaintView扩展了视图{
布局参数;
路径路径=新路径();
油漆br=新油漆();
公共PaintView(上下文){
超级(上下文);
br.设置颜色(颜色为绿色);
br.设置样式(油漆、样式、笔划);
br.设置行程连接(油漆、连接、圆形);
br.设定行程宽度(10f);
params=新的LayoutParams(LayoutParams.MATCH\u父级,LayoutParams.MATCH\u父级);
}
@凌驾
公共布尔onTouchEvent(运动事件){
float x=event.getX();
float y=event.getY();
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
路径移动到(x,y);
返回true;
case MotionEvent.ACTION\u移动:
线路图(x,y);
打破
}
后验证();
返回false;
}
@凌驾
受保护的void onDraw(画布){
canvas.drawPath(路径,br);
}
}
这是我的主要活动课:
包com.example.paint;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.util.AttributeSet;
导入android.view.ViewGroup;
导入android.widget.ImageView;
导入androidx.appcompat.app.appcompat活动;
导入androidx.constraintlayout.widget.constraintlayout;
导入com.example.paint.PaintView;
公共类MainActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
PaintView pv=新的PaintView(本);
setContentView(pv);
ConstraintLayout ConstraintLayout=新ConstraintLayout(此);
//实例化ImageView并定义其属性
ImageView i=新的ImageView(本);
i、 setImageResource(R.drawable.worksheetblank);
//i.setContentDescription(getResources().getString(R.string.worksheetblank));
//设置ImageView边界以匹配可绘制对象的尺寸
i、 setAdjustViewBounds(真);
i、 setLayoutParams(新建ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_内容,
ViewGroup.LayoutParams.WRAP_CONTENT);
//将ImageView添加到布局中,并将布局设置为内容视图。
constraintLayout.addView(i);
setContentView(constraintLayout);
}
}
这里的颂歌
我还想知道我是否可以在java绘图类中显示该矩形?