Custom ImageView doesn't display a Bitmap - android

I'm making a simple compass view that's displayed on top of a MapView. Layout for the MapView and other views are declared in a layout xml file.
I'm using this article as an example.
I've written onDraw method for the compass view but it doesn't display anything. Compass object gets invalidated in onSensorChanged method. Why isn't the picture displayed?
XML layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.mapviews.MyCompass
android:id="#+id/compass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp" />
<com.mapviews.TransparentPanel
android:id="#+id/transparent_panel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="3dp"
android:padding="5dp" />
<ImageButton
android:id="#+id/map_zoom_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:contentDescription="#string/cd_zoom_out"
android:src="#drawable/zoom_out" />
<ImageButton
android:id="#+id/map_zoom_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/map_zoom_out"
android:contentDescription="#string/cd_zoom_in"
android:src="#drawable/zoom_in" />
<ImageButton
android:id="#+id/map_center_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/map_zoom_in"
android:contentDescription="#string/cd_center_user"
android:src="#drawable/directions" />
<com.mapviews.MyMap
android:id="#+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="MyAPIKey"
android:clickable="true"
android:state_enabled="true" />
Part of MyCompass class
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.TRANSPARENT);
canvas.rotate(
-1 * Values.accelValues[0]
, Values.width / 10
, Values.height / 10);
canvas.drawBitmap(
bitmap
, Values.width / 10 - bitmap.getWidth() / 10
, Values.height / 10 - bitmap.getHeight() / 10
, paint);
}
public void setup(Context c) {
paint = new Paint();
bitmap = BitmapFactory.decodeResource(c.getResources(), R.drawable.compass1);
}
In onCreate method
compass = (MyCompass) findViewById(R.id.compass);
compass.setup(this);
compass.bringToFront();

Related

multiple bitmaps drawn while rotating

I have an activity that rotates png image by the SeekBar progress. And it draws me multiple needles. why it does that?
The needle and the speedometer are not perfect, they are just for testings :)
In onCreate:
#Override
public void onCreate(Bundle savedInstanceState) {
...
iv = (ImageView)findViewById(R.id.imageView1);
bMap = BitmapFactory.decodeResource(getResources(), R.drawable.rodykle);
workingBitmap = Bitmap.createBitmap(bMap);
mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
yourCanvas = new Canvas(mutableBitmap);
later i use this void
public void drawneedle(){
yourCanvas.rotate(speed, mutableBitmap.getWidth()/2, mutableBitmap.getHeight()/2);
yourCanvas.save(Canvas.MATRIX_SAVE_FLAG);
yourCanvas.drawBitmap(mutableBitmap, 0,0, null);
yourCanvas.restore();
iv.setImageBitmap(mutableBitmap);
}
Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mtr="http://schemas.android.com/apk/res/com.mindtherobot.samples.thermometer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:orientation="vertical" >
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:max="270" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/spidometras" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
</RelativeLayout>
</LinearLayout>
Do you clear the inner circle of the previously drawn content?
You can do that with canvas.drawColor(0, Mode.CLEAR);
Also, in my opinion, you need to add the configChanges flag to your activity's tag in the AndroidManifest.xml file, to prevent onCreate to run more than once when activity rotates.
android:configChanges="orientation|screenLayout|screenSize|smallestScreenSize"
To know that the activity rotated, you need to override the
onConfigurationChanged(Config newConfig)
method and check the new orientation of the newConfig variable

TranslateAnimation move tomake other views appear

I'm using a TranslateAnimation to make a fragment (GoogleMap) sliding down to give space to an EditText and a TextView to be visible.
so I used this:
text: TextView
edit: EditText
MapLayout: a LinearLayout that contains the Map
Animation animation = new TranslateAnimation(
MapLayout.getX(), MapLayout.getY(),MapLayout.getY(), text.getHeight()+edit.getHeight());
The problem is that I can't make the slide because text.getHeight()+edit.getHeight() returns 0 so there's no slide!
I tried using a number (100 for exemple), the slide is made, but it's different between the devices, I tested on a Galaxy S3 and the slide is not complete, there's still a part of the EditText which is not visible, as for the emulator it worked ok.
When I tried to make the number a bit bigger, so the slide will be longer (200 for exemple), well... the slide was good for the S3, but i was big for the emulator.
So what I want to know is that if there's any way to make the slide move to a point, without depending on the device, I mean without using pixels; so the slide will work perfectly in any device/
I hope that my problem is clear.
Thank you
Update: I don't if this will help, I added a Toast message, show the height of the EditText and the TextView, in the Emulator it says: 85 and in the S3 it says 181
So yeah, I need to make the map slide down in any device like I said
MainActivity:
protected Animation animation;
protected LinearLayout MapLayout;
protected EditText edit;
protected TextView text;
MapLayout = (LinearLayout)findViewById(R.id.MapLayout);
edit = (EditText)findViewById(R.id.Recherche);
text = (TextView)findViewById(R.id.CaptionRecherche);
Toast.makeText(context, "Height: "+(edit.getHeight()+text.getHeight()), 1000).show();
animation = new TranslateAnimation(MapLayout.getX(), MapLayout.getY(), MapLayout.getY(), text.getHeight()+edit.getHeight());
animation.setDuration(1000);
animation.setFillAfter(true);
MapLayout.startAnimation(animation);
Main XML:
------- I'm using a DrawerLayout...I have a slide menu tu show in the application...just for your information-------
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
android:id="#+id/ContenuPrincipal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/activity_main_relative"
/>
<!-- ListView... La liste des options du menu -->
<ListView
android:id="#+id/Menu"
android:layout_width="250dp"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:layout_gravity="start"
android:background="#333"
android:divider="#666"
android:dividerHeight="1dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
/>
</android.support.v4.widget.DrawerLayout>
Main2 XML (The one I included above):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E8E8E8">
<!-- Champs de saisie pour effectuer la recherche: -->
<TextView
android:id="#+id/CaptionRecherche"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Entrer l'emplacement que vous cherchez: "
android:textSize="20sp"
android:layout_marginTop="7dp"
android:layout_marginLeft="20dp"
/>
<EditText
android:id="#+id/Recherche"
android:layout_width="250dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="10dp"
android:hint="Salle, Deparetement..."
android:layout_marginLeft="20dp"
android:layout_marginBottom="20dp"
android:maxLength="100"
android:maxLines="1"
android:layout_below="#id/CaptionRecherche"/>
<!-- La map: -->
<LinearLayout
android:id="#+id/MapLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
</RelativeLayout>
As a part of my application, I have a status bar which contains some text. This status bar is hidden until the user clicks a button at which point it slides down (hiding the topmost content of the layout below).
The code I use to get the correct height of the hidden status bar:
private int hiddenStatusHeight;
private int currentStatusBarHeight;
private void getStatusBarHeight() {
final ViewTreeObserver observer = hiddenStatus.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#SuppressLint("NewApi") #SuppressWarnings("deprecation") #Override public void onGlobalLayout() {
hiddenStatus.measure(MeasureSpec.UNSPECIFIED,
MeasureSpec.UNSPECIFIED);
hiddenStatusHeight = hiddenStatus.getMeasuredHeight();
currentStatusBarHeight = statusBar.getHeight();
ViewTreeObserver obs = hiddenStatus.getViewTreeObserver();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
obs.removeOnGlobalLayoutListener(this);
} else {
obs.removeGlobalOnLayoutListener(this);
}
}
});
}
The code that is executed when the button is clicked:
private OnClickListener ExpandClickListener = new OnClickListener() {
#Override public void onClick(View v) {
boolean isExpanded = (Boolean) expandButton
.getTag(R.id.TAG_EXPANDED);
int originalHeight = (Integer) expandButton
.getTag(R.id.TAG_ORIGINAL_HEIGHT);
if (isExpanded) {
expandButton.setTag(R.id.TAG_EXPANDED, false);
expandButton.setImageResource(R.drawable.ic_action_down);
// statusBar.setLayoutParams(new FrameLayout.LayoutParams(
// LayoutParams.MATCH_PARENT, originalHeight));
Log.d(TAG, "Collapsing to " + originalHeight);
ValueAnimator va = ValueAnimator.ofInt(currentStatusBarHeight,
originalHeight);
va.setDuration(500);
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
statusBar.getLayoutParams().height = value.intValue();
statusBar.requestLayout();
}
});
va.start();
} else {
expandButton.setTag(R.id.TAG_EXPANDED, true);
expandButton.setImageResource(R.drawable.ic_action_collapse);
currentStatusBarHeight = originalHeight + hiddenStatusHeight;
// statusBar.setLayoutParams(new FrameLayout.LayoutParams(
// LayoutParams.MATCH_PARENT, currentStatusBarHeight + 15));
Log.d(TAG, "Expanding to " + originalHeight + "+"
+ hiddenStatusHeight + "=" + currentStatusBarHeight);
ValueAnimator va = ValueAnimator.ofInt(originalHeight,
currentStatusBarHeight);
va.setDuration(500);
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
statusBar.getLayoutParams().height = value.intValue();
statusBar.requestLayout();
}
});
va.start();
}
}
};
And finally my layout XML (it has to be a FrameLayout):
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="#+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="70dp" >
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:showDividers="middle" >
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/displayStatusBar"
style="#style/DisplayStatusBar"
android:layout_width="match_parent"
android:layout_height="65dp" >
<RelativeLayout
android:id="#+id/status_always_visible"
style="#style/StatusBar"
android:layout_width="match_parent"
android:layout_height="20dp" >
<TextView
android:id="#+id/status_received"
style="#style/StatusBarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/received" />
<TextView
android:id="#+id/status_time_received"
style="#style/StatusBarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/status_received" />
<TextView
android:id="#+id/status_time_delete_relative_text"
style="#style/StatusBarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/status_time_delete_relative"
android:text="#string/is_deleted" />
<TextView
android:id="#+id/status_time_delete_relative"
style="#style/StatusBarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/minutes" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/status_hidden"
style="#style/StatusHidden"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/status_always_visible" >
<LinearLayout
android:id="#+id/status_hydrants_near_address_container"
style="#style/StatusHiddenText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:divider="#android:drawable/divider_horizontal_bright"
android:orientation="vertical"
android:paddingLeft="10dp"
android:showDividers="middle" >
<TextView
style="#style/StatusHiddenText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_information" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/optionsBar"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#999"
android:orientation="horizontal"
android:paddingTop="5dp" >
<ImageButton
android:id="#+id/button_hydrants"
style="#style/android:Widget.ImageButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:alpha="50"
android:contentDescription="#string/module_hydrants"
android:src="#drawable/ic_action_place" />
<ImageButton
android:id="#+id/button_route"
style="#style/android:Widget.ImageButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:contentDescription="#string/module_directions"
android:src="#drawable/ic_action_directions" />
<ImageButton
android:id="#+id/button_pdf"
style="#style/android:Widget.ImageButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:clickable="false"
android:contentDescription="#string/module_accessplan"
android:src="#drawable/ic_action_attachment" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageButton
android:id="#+id/button_more"
style="#style/android:Widget.ImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_action_down" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
<!-- The "empty" view to show when there are no items in the "list" view defined above. -->
<TextView
android:id="#android:id/empty"
style="?android:textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="32dp"
android:text="#string/no_information"
android:textColor="?android:textColorSecondary" />
</FrameLayout>
Hope some of this may be helpful to you.
Just to mention it, I have asked a similar question where the visible layout is pushed downwards as the menu expands. So have a look at that if you rather want this behaviour: How to animate a slide in notification view that pushes the content view down
Happy coding

Adjust view width and height to 50%

I'm creating a gallery with custom adapter (a relative layout in witch i place an imageview as a thumbnail, a textview as Name and other staff).
The gallery displays in fullscreen perfectly, but this is not what i want.. i want the custom relative layout to be scaled to 50% in height and width relatively to the screen size. I tried this :
gallery.getLayoutParams().width = relativelayout.getWidth() / 2;
gallery.getLayoutParams().height = relativelayout.getHeight() / 2;
This was a wrong try, it just set the height and width to the correct size without scaling the content.
This is the original view :
This is what i've expected :
And this is what i've got :
This is the layout Cover :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/ivTourCover"
android:layout_width="640dp"
android:layout_height="240dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/app_name"
android:scaleType="fitXY"
android:src="#drawable/croppedstopgallery_f45d88e4_7864_4dbc_9123_ad7953d8724a" />
<ImageButton
android:id="#+id/ivHorizontalLine"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/tvTourName"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="0dp"
android:contentDescription="#string/app_name"
android:scaleType="fitXY"
android:src="#drawable/horizontal_line" />
<TextView
android:id="#+id/tvAuthorName"
style="#style/Cover.AuthorName"
android:layout_width="fill_parent"
android:layout_height="19dp"
android:layout_alignTop="#+id/ivAuthorThumbnail"
android:layout_marginLeft="12dp"
android:layout_marginRight="17dp"
android:layout_toLeftOf="#+id/ivGo"
android:layout_toRightOf="#+id/ivAuthorThumbnail"
android:text="Anna Greenleaf" />
<ImageView
android:id="#+id/ivAuthorThumbnail"
android:layout_width="46dp"
android:layout_height="46dp"
android:layout_alignLeft="#+id/ivHorizontalLine"
android:layout_below="#+id/ivHorizontalLine"
android:layout_marginTop="20dp"
android:contentDescription="#string/app_name"
android:scaleType="fitXY"
android:src="#drawable/croppedstopgallery_f45d88e4_7864_4dbc_9123_ad7953d8724a" />
<ImageView
android:id="#+id/ivGo"
android:layout_width="46dp"
android:layout_height="46dp"
android:layout_alignRight="#+id/ivHorizontalLine"
android:layout_alignTop="#+id/tvAuthorName"
android:contentDescription="#string/app_name"
android:scaleType="fitXY"
android:src="#drawable/tour_cover_go" />
<TextView
android:id="#+id/tvAuthorDescription"
style="#style/Cover.AuthorDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvAuthorName"
android:layout_alignRight="#+id/tvAuthorName"
android:layout_below="#+id/tvAuthorName"
android:text="London native and London over!" />
<SurfaceView
android:id="#+id/svFooter"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#color/CoverFooter" />
<TextView
android:id="#+id/tvFooterText"
style="#style/Cover.FooterText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/svFooter"
android:layout_alignBottom="#+id/svFooter"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:text="Individual" />
<com.example.zipdownload.utilities.ui.AutoResizeTextView
android:id="#+id/tvTourName"
style="#style/Cover.TourName"
android:layout_width="wrap_content"
android:layout_height="79dp"
android:layout_below="#+id/ivTourCover"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="15dp"
android:gravity="center|top"
android:text="Beautiful London in Winston Churchill&apos;s footsteps" />
</RelativeLayout>
The View Gallery :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rlGallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Gallery" >
<Gallery
android:id="#+id/gCoverflow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="top" />
</RelativeLayout>
And the Activity Gallery :
public class Gallery extends Activity {
private android.widget.Gallery gCoverflow;
private RelativeLayout rlGallery;
private CoverAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
rlGallery = (RelativeLayout) findViewById(R.id.rlGallery);
adapter = new CoverAdapter(this);
gCoverflow = (android.widget.Gallery) findViewById(R.id.gCoverflow);
gCoverflow.setAdapter(adapter);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
gCoverflow.getLayoutParams().width = rlGallery.getWidth() / 2;
gCoverflow.getLayoutParams().height = rlGallery.getHeight() / 2;
}
}
You can try to use ScaleAnimation but in this case touch events will work incorrect:
final ScaleAnimation animation = new ScaleAnimation (
1f, 0.5f, 1f, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f
);
final LayoutAnimationController controller = new LayoutAnimationController(animation);
animation.setDuration(0);
animation.setFillAfter(true);
gCoverflow.setLayoutAnimation(controller);
This code should be added in onCreate method.
I found it guys, is stead of :
gCoverflow.getLayoutParams().width = rlGallery.getWidth() / 2;
gCoverflow.getLayoutParams().height = rlGallery.getHeight() / 2;
I user this :
gCoverflow.setScaleX((float) 0.5);
gCoverflow.setScaleY((float) 0.5);

Drawing a new view into a relative layout

We are writing a "dashboard" for a car. The code for calculating/drawing the speedometer was done separate from the rest of the code, and now I am trying to combine the two.
They both work separately, and with the speedometer view code in, the main portion of the UI is still functioning, I just can't see the speedometer.
I have it compiling, and there are no obvious errors. However, when the app is run, I don't see the speedometer showing up in the main view, even though I have added it using the addView method.
For Reference:
My code to create and add the view:
setContentView(R.layout.main);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.RelativeLayout1);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.CENTER_VERTICAL);
myView v = new myView(this);
v.setLayoutParams(lp);
relativeLayout.addView(v, lp);
The code for the myView class:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.view.View;
public class myView extends View {
private Paint mPaints;
private Paint textpaint;
private boolean mUseCenters;
private RectF mBigOval;
private float mStart;
private float mSweep;
public float SPEED_raw;
public int SPEED = 0; //initialized to 0 just for loop. Remove initialization when IF statement below is deleted
public static Bitmap gaugefront;
public static Bitmap gaugeback;
public myView(Context context) {
super(context);
mPaints = new Paint();
mPaints.setAntiAlias(true);
mPaints.setColor(Color.YELLOW);
textpaint = new Paint();
textpaint.setAntiAlias(true);
textpaint.setColor(Color.WHITE);
textpaint.setTextSize(150);
textpaint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
gaugefront = BitmapFactory.decodeResource(context.getResources(),R.drawable.gaugefront); //Load gaugefront.png
gaugeback = BitmapFactory.decodeResource(context.getResources(),R.drawable.gaugeback); //Load gaugeback.png
mUseCenters = true;
mBigOval = new RectF(400, 10, 880, 490); //left[x-coordinate],top[y-coordinate],right[x],bottom[y]
}
private void drawArcs(Canvas canvas, RectF oval, boolean useCenter, Paint paint) {
canvas.drawArc(oval, mStart, mSweep, useCenter, paint);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(myView.gaugeback, 400, 10, null); //draw gauge background, X coordinate:400, Y coordinate: 10
drawArcs(canvas, mBigOval, mUseCenters, mPaints);
//SPEED_raw = 70; //Raw float data from CCS. Uncomment when the IF statement below is deleted.
SPEED = Math.round(SPEED_raw); //SPEED integer. Units km/h - Top speed of 135. Used for digital readout
mStart = 90; //Start drawing from -90 degrees (Counter clockwise)
mSweep = SPEED_raw*2; //Draw stop point
if(SPEED >= 135){ //JUST FOR SHOW. Delete this for actual speedo
SPEED_raw = 0; // "
} // "
else // "
SPEED_raw += 0.5; // "
canvas.drawBitmap(myView.gaugefront, 400, 10, null); //draw gauge foreground, X coordinate:400, Y coordinate: 10
String speed_string = String.valueOf(SPEED); //Convert int SPEED to String for display
// while (speed_string.endsWith(".0") || speed_string.endsWith(".")){ //Erase trailing ".0" for float types. Don't need if SPEED is an int
// speed_string = (speed_string.substring(0, speed_string.length() - 1));
// }
canvas.drawText(speed_string, 640, 420, textpaint); //arguments: (string, x-coordinate, y-coordinate, paint)
invalidate();
}
}
Here's my XML File:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:keepScreenOn="true"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/solarbg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/solarbg" />
</LinearLayout>
<ImageView
android:id="#+id/l_lamp"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_gravity="left"
android:src="#drawable/l_arrow_dim" />
<DigitalClock
android:id="#+id/digitalClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="50dp" />
<ImageView
android:id="#+id/r_lamp"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/r_arrow_dim" />
<TextView
android:id="#+id/setAmps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="left"
android:text="#string/setAmps"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/setAmpsVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_below = "#+id/setAmps"
android:layout_alignParentLeft="true"
android:maxEms = "5"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/setVelocity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/setAmps"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="right"
android:text="#string/setVelocity"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/setVelocityVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_below = "#+id/setVelocity"
android:layout_alignParentRight="true"
android:maxEms = "5"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/actVelocity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/setVelocityVal"
android:gravity="right"
android:text="#string/actVelocity"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/actVelocityVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below = "#+id/actVelocity"
android:layout_alignParentRight="true"
android:gravity="right"
android:maxEms = "5"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/busAmps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/setAmpsVal"
android:gravity="left"
android:text="#string/busAmps"
android:textColor="#ffffff"
android:textSize="30dp" /><TextView
android:id="#+id/busAmpsVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_below = "#+id/busAmps"
android:maxEms = "5"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/powerVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom = "true"
android:gravity="right"
android:textColor="#ffffff"
android:textSize="30dp" />
<TextView
android:id="#+id/power"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_above = "#+id/powerVal"
android:gravity="left"
android:text="#string/power"
android:textColor="#ffffff"
android:textSize="30dp" />
<TableLayout
android:id="#+id/msgCenterLayout"
android:layout_width="600dp"
android:layout_height="200dp"
android:layout_alignParentBottom = "true"
android:layout_centerHorizontal = "true"
android:orientation = "vertical"
android:visibility = "invisible">
<ScrollView
android:id="#+id/messageCenterText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left"
android:text="#string/busAmps"
android:textColor="#ffffff"
android:textSize="30dp" />
</TableLayout>
</RelativeLayout>
edit:
I'm using an XML file for the main layout, and trying to add this new view to a relativelayout inside that XML file.
Try writing
LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT);
v.setLayoutParams(lp);
relativeLayout.addView(v);

How to refresh a customized view in a layout

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.

Categories

Resources