基本上,我已经创建了一个自定义类,该类扩展了View,它目前正在绘制一条水平线,在该水平线处触摸屏幕,直到释放屏幕为止。 我要实现的是在背景中的行后添加ImageView,以便在前景中绘制线。这是问题所在,尽管进行了所有研究,但我不知道如何在此视图中添加ImageView。
根据我的理解,重写的onDraw函数需要:#1清除屏幕->#2在ImageView上绘制图像->#3在每次迭代中绘制线条。如果此信息有某种帮助,我将不得不稍后在所说的ImageView上实现动态图像更改。
public class RateView extends View{
float touchY = (getHeight() / 2);
boolean isPressed = false;
Paint paint=new Paint();
public RateView(Context context, AttributeSet attrs) {
super(context);
this.setBackgroundColor(Color.parseColor("#da5f02"));
init(null);
}
private void init(AttributeSet attr){
paint.setColor(Color.GREEN);
paint.setStrokeWidth(10);
paint.setAntiAlias(true);
}
@Override
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
if (isPressed){
canvas.drawLine(5, touchY, (getWidth()-10), touchY,paint);
}
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent){
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
isPressed = true;
break;
case MotionEvent.ACTION_MOVE:
touchY = motionEvent.getY();
break;
case MotionEvent.ACTION_UP:
isPressed = false;
break;
}
invalidate();
return true;
}}
这是我的costum视图类。我的主类仅扩展AppCompatActivity并将contentView设置为上述视图。非常感谢您提前答复!
EDIT: MainActivity
public class MainScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rateView = new RateView(this, null);
setContentView(rateView);
}
}
EDIT: MainActivity XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainScreen"
android:background="#da5f02">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Picture"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:background="#da5f02"
android:src="@drawable/monalisa"
android:adjustViewBounds="true"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainScreen"
android:background="#da5f02">
<com.yourpackage.RateView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Picture"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:background="#da5f02"
android:src="@drawable/monalisa"
android:adjustViewBounds="true"/>
</RelativeLayout>
请看这个
公共类MainScreen扩展了AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rateView = new RateView(this, null);
setContentView(R.layout.activity_main);
}
}
如果您要自定义视图,则只能为视图添加一个背景,因此您应该更改实现上述成就的方法。
public class RateView extends View
在“活动/片段”中添加此视图在activity_main中放置
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="5dp"
android:src="@drawable/image" />
<com.yourpackage.RateView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal" />
</LinearLayout>
在此处添加图像视图无法添加图像。