0

I have a android.graphics.Path variable which I want to convert into an ImageView in my activity. What I do right now and does NOT work (but it does run without error, it just displays a blank activity) is:

public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Path p = ... // some bezier curves PathShape pathShape = new PathShape(path, (float)20.0, (float)20.0); ShapeDrawable shapeDrawable = new ShapeDrawable(pathShape); ImageView testImage = (ImageView) findViewById(R.id.testImage); testImage.setImageDrawable(shapeDrawable); } } 

The corresponding activity_main.xml is:

<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.tg.MindGames.set.MainActivity">

<ImageView android:id="@+id/testImage" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 

</RelativeLayout>

What did I do wrong and what is considered best practice to put Path into an ImageView?

Additional Information: I want to use the Paths to draw the images of cards in a simple card game. Several cards and additional text fields should be displayed on the activity at the same time, cards should be able to have an onClickListener. The size of 20.0 x 20.0 is arbitrary and should be altered later when I use the ImageViews in a GridView.

10
  • you have to sprcify correct Paint for your shape Commented Sep 3, 2014 at 12:04
  • I cannot figure out how to get from the Path to a Paint and from the Paint to a Drawable for the ImageView Commented Sep 3, 2014 at 12:23
  • see ShapeDrawable methods Commented Sep 3, 2014 at 12:27
  • Now I tried several things with Paints, ShapeDrawables, Canvas,... but nothing worked...can you please give me some example code? Commented Sep 3, 2014 at 12:49
  • tried ShapeDrawable.getPaint()? Commented Sep 3, 2014 at 12:52

1 Answer 1

2

so here you have a red triangle drawn on the view's background:

View v = new View(this); Path path = new Path(); path.moveTo(0, 1); path.lineTo(0.5f, 0); path.lineTo(1, 1); ShapeDrawable sd = new ShapeDrawable(new PathShape(path, 1, 1)); sd.getPaint().setColor(Color.RED); v.setBackgroundDrawable(sd); setContentView(v); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.