Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add View to XML layout android

I have a XML layout that contain all my buttons and images and i want a moving cloud on the top of my layout. so i created a view and made my cloud move, however i couldnt link the view with layout. here is my view code

public class CloudView extends View {

Bitmap cloud;
int ChangingX;


public CloudView(Context context) {
    // TODO Auto-generated constructor stub
    super(context);
    cloud = BitmapFactory.decodeResource(getResources(), R.drawable.cloud);
    ChangingX = 50;

}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    canvas.drawBitmap(cloud, ChangingX , 50, null);
    if (ChangingX < canvas.getWidth())
        ChangingX += 2;
    else
        ChangingX = 50;
    invalidate();
}

}

and here is my MainActivity

public class MainActivity extends Activity {

CloudView myView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    myView = new CloudView(this);
    setContentView(myView);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

 }

Im new at animation in android can u explain in details how i can link a View with layout. and if it wont work what other classes besides View that i can use.

Thanks for your time and consideration. and sorry for my bad English.

like image 242
Coderji Avatar asked Oct 03 '22 21:10

Coderji


1 Answers

Here is Android Developer Link might me useful for you.

Define Custom Attributes

How to define attribute :

To define custom attributes, add resources to your project. It's customary to put these resources into a res/values/attrs.xml file. Here's an example of an attrs.xml file:

<resources>
   <declare-styleable name="PieChart">
       <attr name="showText" format="boolean" />
       <attr name="labelPosition" format="enum">
           <enum name="left" value="0"/>
           <enum name="right" value="1"/>
       </attr>
   </declare-styleable>
</resources>

How to use in XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews">
 <com.example.customviews.charting.PieChart
     custom:showText="true"
     custom:labelPosition="left" />
</LinearLayout>

Read for more details do follow.

like image 131
Chintan Khetiya Avatar answered Oct 07 '22 18:10

Chintan Khetiya