How to refresh a customized view in a layout - android

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.

Related

Canvas view not going below any view

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>

Can't paint on surfaceview

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>

Why won't my canvas show up (Android)?

I'm writing a program that is supposed to draw a line in a canvas at the bottom of the screen below a button, etc. My layout shows up, but the line doesn't. Anyone know why my canvas doesn't show up?
Here is the code:
public class Vectors extends Activity{
VectorsView vectorsView;
LinearLayout l;
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.vectors);
vectorsView = new VectorsView(this);
l = (LinearLayout) findViewById(R.id.llCanvasV);
l.addView(vectorsView);
.......
}
public class VectorsView extends View{
public VectorsView(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawLine(0, 0, 100, 100, paint);
vectorsView.draw(canvas);
vectorsView.setLayoutParams(new LayoutParams(25, 25));
}
}
Here is the xml:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background"
android:orientation="vertical">
<ImageView android:layout_height="wrap_content"
android:src="#drawable/vectors"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:adjustViewBounds="true">
</ImageView>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<Button android:text="Choose Program"
android:id="#+id/bChsProgV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="97dp"
android:adjustViewBounds="true">
</Button>
<ImageButton android:layout_height="wrap_content"
android:src="#drawable/help"
android:id="#+id/ibHelpV"
android:layout_width="wrap_content"
android:layout_marginLeft="65dp"
android:background="#null"
android:layout_marginTop="10dp">
</ImageButton>
</LinearLayout>
<LinearLayout android:id="#+id/llCanvasV"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</LinearLayout>
</LinearLayout>
onMeasure method missing in VectorsView class. and also you can add VectorsView from Layout by keeping the complete package <package.VectorsView android:id= android:width= android:height= ></package.VectorsView>

Android upside down a layout or rotate by 180

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

Custom view made not visible the elements above in xml

i have a custom view to show a gif animated image. The problem born when i put in my linearlayout the custom view, the other elements under my custom view aren't displayed. this is my view:
public class GIFView extends View{
private Movie movie;
private InputStream is;
private long moviestart;
private long duration;
public GIFView(Context context) {
super(context);
is=getResources().openRawResource(R.drawable.anim_cerca);
movie=Movie.decodeStream(is);
duration = movie.duration();
}
public GIFView(Context context,AttributeSet set){
super(context,set);
is=getResources().openRawResource(R.drawable.anim_cerca);
movie=Movie.decodeStream(is);
duration = movie.duration();
}
public GIFView(Context context,AttributeSet set,int def){
super(context,set,def);
is=getResources().openRawResource(R.drawable.anim_cerca);
movie=Movie.decodeStream(is);
duration = movie.duration();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
long now=android.os.SystemClock.uptimeMillis();
Paint p = new Paint();
p.setAntiAlias(true);
if (moviestart == 0)
moviestart = now;
int relTime = (int)((now - moviestart) % duration);
movie.setTime(relTime);
movie.draw(canvas,0,0);
this.invalidate();
}
}
and this is my layout xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#drawable/sfondo">
<LinearLayout android:id="#+id/linearLayout2"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:orientation="vertical">
<LinearLayout android:id="#+id/linearLayout1"
android:layout_height="fill_parent" android:weightSum="1"
android:layout_width="fill_parent" android:orientation="horizontal">
<ImageView android:layout_height="73dp" android:id="#+id/imageView1"
android:layout_weight="0.25" android:src="#drawable/trovachiavi"
android:layout_width="256dip"></ImageView>
<ImageButton android:id="#+id/infoButton"
android:background="#null" android:layout_height="47dp"
android:layout_marginLeft="10dip" android:layout_weight="0.25"
android:layout_marginTop="15dip" android:src="#drawable/info_mini"
android:layout_width="47dp"></ImageButton>
</LinearLayout>
</LinearLayout>
<LinearLayout android:id="#+id/layoutGIF"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">
</LinearLayout>
<ImageButton android:id="#+id/avvia_cerca"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_marginTop="10dp" android:layout_gravity="center|bottom"
android:background="#null"></ImageButton>
</LinearLayout>
in the activity i add the custom view to layout with id layoutGIF in this way:
LinearLayout lg = (LinearLayout)findViewById(R.id.layoutGIF);
lg.setGravity(Gravity.CENTER);
gif = new GIFView(this);
lg.addView(gif, new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
what can i do? i need the elements under this!
best way is using animation.. don't think that gif is solution.. is not supported from every android device!

Categories

Resources