基本上,我已经创建了一个自定义类,该类扩展了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设置为上述视图。非常感谢您提前答复!
编辑: MainActivity
public class MainScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rateView = new RateView(this, null);
setContentView(rateView);
}
}
编辑: 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);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句