V
V
Victorius2014-01-05 12:46:58
Java
Victorius, 2014-01-05 12:46:58

How to programmatically update an ImageView?

Hey!
I want to make an application that would display certain values, like Voltmeter/Ammeter/Speedometer - i.e. so that the arrow moves along the scale when the value changes.
I decided that I would use an ImageView and draw lines on the canvas.
But when I render on canvas, after that myImageView.invalidate();
The display shows only the arrow in the last position, there is no smooth movement / drawing of the arrow.

public class TesteRotateActivity extends Activity implements SensorEventListener {

private MyImageView myImageView;
private SensorManager mSensorManager;
private int widthIV=120,heightIV=100;
private Sensor sensor;
private Bitmap bitmap = Bitmap.createBitmap(widthIV,heightIV,Bitmap.Config.ARGB_8888);
private Bitmap b2=Bitmap.createBitmap(widthIV,heightIV,Bitmap.Config.ARGB_8888);
private int m,a=(int)widthIV/2,b=(int)heightIV/2;
private Canvas canvas;
private TextView t;
private Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
private double prev = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
    myImageView = (MyImageView) findViewById(R.id.imageview);
    t = (TextView)findViewById(R.id.textview);
    ImageView i2 = (ImageView)findViewById(R.id.i2);
    Button b = (Button)findViewById(R.id.button1);
    b.setOnClickListener(new View.OnClickListener() {
    
    @Override
    public void onClick(View arg0) {
      globalDraw(50);
    }
  });
}
  @Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
  }

  @Override
  public void onSensorChanged(SensorEvent event) {	
  }
  protected void onResume(){
    super.onResume();
    mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
  }
  protected void onPause(){
    super.onPause();
    mSensorManager.unregisterListener(this);
  }
  
  private void drawArraw(double t){
    bitmap=b2.copy(Bitmap.Config.ARGB_8888, true);
    canvas = new Canvas(bitmap);
    p.setColor(Color.DKGRAY);
    p.setStrokeWidth(2f);
      myImageView.setImageBitmap(bitmap);	    
    int x=(int) (a*Math.cos(t))+a;
    int y=(int) (b*Math.sin(t))+b;
    canvas.drawLine(a,b,x, y, p);
    myImageView.setImageDrawable(new BitmapDrawable(getResources(),bitmap));
    myImageView.invalidate();
  }
  public void globalDraw(double valueOfMeter){
    Integer in = (new Double(valueOfMeter)).intValue();
      m=1;
      for(int k =0;k<in.toString().toCharArray().length-2;k++)
      	m*=10;
    double diff = prev-valueOfMeter;
    if(diff<0)
      for(double i=diff;i<=0;i++){
        double value = (valueOfMeter+i)/m*1.8+180;
        value=value*Math.PI/180;
        drawArraw(value);
      }
    else
      for(double i=0;i<diff;i++){
        double value = (prev-i)/m*1.8+180;
        value=value*Math.PI/180;
        drawArraw(value);
      }
  }
}

How should I display the ImageView's changes dynamically, or what tool should I use to achieve the same goal.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Victorius, 2014-01-29
@Victorius

It was necessary to create your own View, rewrite onDraw. A prime example for implementing a speedometer or something similar - GaugeView

M
Moxa, 2014-01-06
@Moxa

use SurfaceView and you will be happy)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question