Reputation: 195
Basically I've created a custom class which extends View and what it does currently is drawing a horizontal line where the screen is being touched until it's released. What I want to achieve is adding an ImageView in the background, behind the line so that line is drawn in the foreground. Here's the problem as I've got no clue how to add an ImageView to this view despite all the researches.
From my understanding the overriden onDraw function needs to: #1 clear the screen -> #2 draw the image on ImageView -> #3 draw the line on each iteration. I will have to later on implement dynamically image changing on said ImageView if this info helps somehow.
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;
}}
This is my costum view class. My main class only extends AppCompatActivity and sets contentView to the above view. Thanks a lot for replys in advance!
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>
Upvotes: 0
Views: 2627
Reputation: 2962
<?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>
Please look at this
public class MainScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rateView = new RateView(this, null);
setContentView(R.layout.activity_main);
}
}
Upvotes: 0
Reputation: 2962
public class RateView extends View
Add this view in Activity/Fragment In activity_main put
<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>
add image here view can not add image.
Upvotes: 0
Reputation: 1709
If you will Custom view then you can add only one background for View so you should change your approach for above achievement.
Upvotes: 1