I'm trying to make simple app that displays rectangle and has ability to change its color by button.
Rectangle class is:
public class DrawView extends View{
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.YELLOW);
canvas.drawRect(300, 550, 150, 400, paint );
}
public void setColorRed()
{
paint.setColor(Color.RED);
invalidate();
}
My app is Tab Layout app. This class is displayed in 3rd Tab this way:
main.xml
<TabHost
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/bRedColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red" />
<com.thms.systemy3.DrawView
android:id="#+id/yourID"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.thms.systemy3.DrawView>
</FrameLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
I was trying to get access to setColorRed() from my MainClass.java class by
DrawView drawview;
and then use
drawview.setColorRed()
MainClass.java:
public class MainClass extends Activity implements OnClickListener{
TabHost th;
DrawView drawview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
th = (TabHost) findViewById(R.id.tabhost);
//tab3
Button bColorRed = (Button) findViewById(R.id.bRedColor);
th.setup();
TabSpec specs = th.newTabSpec("tag1");
specs.setContent(R.id.tab1);
specs.setIndicator("TAB1");
th.addTab(specs);
specs = th.newTabSpec("tag2");
specs.setContent(R.id.tab2);
specs.setIndicator("TAB2");
th.addTab(specs);
specs = th.newTabSpec("tag3");
specs.setContent(R.id.tab3);
specs.setIndicator("TAB3");
th.addTab(specs);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bRedColor:
drawview.setColorRed();
break;
}
What am I doing wrong? Could anyone correct this code or give me a proper example of setting up simple app that draws rectangle and is able to change color by a button?
Thank you for reply.
try using this:
paint.setStyle(Paint.Style.FILL);
Related
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>
If you will Custom view then you can add only one background for View so you should change your approach for above achievement.
<?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);
}
}
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.
My activity is in LANDSCAPE mode
I am using following code.
Activity_main.xml
<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">
<TextView
android:id="#+id/sample"
android:layout_height="80dp"
android:layout_width="match_parent"
android:text="saga"/>
<com.example.sample.Circle
android:id="#+id/circle"
android:background="#AD9999"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/sample"/>
</RelativeLayout>
Circle.java
public class Circle extends View{
public Circle(Context context) {
super(context);
this.context=context;
}
public Circle(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
mOuterDialPaint.setColor(Color.BLUE);
}
#Override
public void onDraw(Canvas canvas)
{
float width=canvas.getWidth()/2;
Log.i("Width", ""+canvas.getHeight());
float dialCenterX=width;
canvas.drawCircle(dialCenterX, canvas.getHeight(), dialCenterX, mOuterDialPaint);
}
}
So canvas is drawing circle from specified positions but not moving below textview so textview is coming on top of canvas and some part is getting cut. So any fix for this problem??
Use frame-Layout in xml:
<FrameLayout 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" >
<com.example.sample.Circle
android:id="#+id/circle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#AD9999" />
<TextView
android:id="#+id/sample"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="top"
android:gravity="center"
android:text="your text view" />
</FrameLayout>
I'm trying to use SurfaceView and want to paint on it, use code:
public class TestPaint extends SurfaceView {
public TestPaint(Context context) {
super(context);
setWillNotDraw(false);
setBackgroundColor(color.white);
}
#Override
protected void onDraw(Canvas canvas) {
Paint p = new Paint();
p.setColor(color.black);
canvas.drawText("test", 10, 10, p);
}
}
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TestPaint p=new TestPaint(this);
p.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
LinearLayout rl=(LinearLayout) findViewById(R.id.rltMain);
rl.addView(p);
}
}
The TestPaint display black background only. I try Why do I get it?
Using paint class better then that you have use frame layout and set text view or whatever u want.
Check this,
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<SurfaceView
android:id="#+id/surfaceView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</SurfaceView>
</LinearLayout>
<TextView
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:text="test"
android:background="#color/abc_search_url_text_normal"
android:textColor="#android:color/black"
android:textSize="20sp" />
</FrameLayout>
I am coding an Android game in which 2 players play at the same time. One player faces the phone in a normal way and the other faces it upside down.
As it is a quiz, I didn't use any canvas or graphics. It contains only 2 linear layouts and one of them is supposed to be upside down. For this I have used:
android:rotation="180"
for one of the layouts.
it showed upside down on the graphical view of my xml in Eclipse but when I run it on an emulator or a phone it is not inverted or rotated to 180 degrees.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:clipToPadding="false"
android:rotation="180">
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</LinearLayout
Java Code
public class x extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
How can I rotate it without complication. I am presently using api level 15 but i have no restriction on API level.
I would suggest that you create a custom Layout that will handle the rotation on it's onDraw()
public class RotatedLinearLayout extends RotatedLinearLayout {
final boolean topDown;
public RotatedLinearLayout (Context context){
super(context);
}
public RotatedLinearLayout (Context context, AttributeSet attrs){
super(context, attrs);
}
public RotatedLinearLayout (Context context, AttributeSet attrs, int defStyle){
super(context, attrs,defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
#Override
protected void onDraw(Canvas canvas){
TextPaint textPaint = getPaint();
textPaint.setColor(getCurrentTextColor());
textPaint.drawableState = getDrawableState();
canvas.save();
if(topDown){
canvas.translate(getWidth(), 0);
canvas.rotate(180);
}else {
canvas.translate(0, getHeight());
canvas.rotate(-180);
}
canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());
getLayout().draw(canvas);
canvas.restore();
}
}
When you write your XML, use the VerticalRelativeLayout instead of the other layout you tried to create.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<path.to.package.RotatedLinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:clipToPadding="false"
>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</path.to.package.RotatedLinearLayout >
</LinearLayout
I just created a view for displaying PDF file
public class PDFGraphic extends View{
String mText;
float mLastX;
float mLastY;
public float mOffX;
public float mOffY;
Canvas mCan;
Bitmap mBi;
public PDFGraphic(Context context, AttributeSet attrs){
super(context,attrs);
setPageBitmap();
setBackgroundColor(Color.TRANSPARENT);
}
public void uiInvalidate() {
postInvalidate();
}
public void setPageBitmap() {
mBi = Bitmap.createBitmap(100, 100, Config.RGB_565);
mCan = new Canvas(mBi);
mCan.drawColor(Color.RED);
}
public void onDraw(Canvas canvas) {
Paint paint = new Paint();
canvas.drawBitmap(mBi, 0, 0, paint);
}
}
And this is my xml file(pdfview):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<RelativeLayout android:id="#+id/relativeLayout1" android:layout_width="match_parent" android:gravity="bottom|center" android:layout_height="wrap_content" android:minHeight="30px" android:visibility="visible">
<com.shawnprojectPDF.PDFGraphic android:id="#+id/pdfview1" android:layout_width="match_parent" android:layout_above="#+id/editText1" android:layout_alignParentTop="true" android:layout_height="match_parent"></com.shawnprojectPDF.PDFGraphic>
<ImageButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="#drawable/leftarray" android:id="#+id/left" android:layout_alignParentBottom="true" android:layout_marginLeft="68px"></ImageButton>
<EditText android:id="#+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="#+id/left" android:layout_alignParentBottom="true" android:gravity="center" android:width="100px" android:singleLine="true" android:maxLength="12"></EditText>
<ImageButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/right" android:src="#drawable/rightarray" android:layout_toRightOf="#+id/editText1" android:layout_alignParentBottom="true"></ImageButton>
</RelativeLayout>
</LinearLayout>
In my main activity,if setContentView(R.Layout.pdfview), the view(PDFGraphic) will not be invalidated, if setContentView(New PDFGraphic(this)),it invalidates successfully.
How to refresh the view in the whole layout.
I don't see the problem. These are the results that I get with your code (next time, you do this part). With setContentView(R.layout.pdfview), this is the result:
With setContentView(new PDFGraphic(this, null) -- note that I used the two-arg constructor because you didn't define PDFGraphic(Context) -- this is the result:
In either case, your onDraw() is happening correctly.