PAM Lab 4 Rotaru Dan [PDF]

  • Author / Uploaded
  • Dan
  • 0 0 0
  • Gefällt Ihnen dieses papier und der download? Sie können Ihre eigene PDF-Datei in wenigen Minuten kostenlos online veröffentlichen! Anmelden
Datei wird geladen, bitte warten...
Zitiervorschau

Ministerul Educaţiei, Culturii și Cercetării al Republicii Moldova Universitatea Tehnică a Moldovei Departamentul Ingineria Software și Automatică

RAPORT Lucrare de laborator Nr.4 Disciplina: Programarea aplicațiilor mobile Tema: Custom Progress/Loading Bar

A efectuat:

st.gr. TI-195 Rotaru Dan

A verificat :

asist. univ., Cristian Rusu

Chișinău 2021

Scopul lucrării de laborator Realizarea unui control grafic de vizualizare a progresului cu implementarea lui în practică. Se va alege la dorința dezvoltatorului o animație GIF sau Custom care va fi implementata în cod. Condiții: • • • •

Elementele grafice rasteriale vor fi convertate în elemente de tip vectorial. Cotrolul grafic va fi suprapus pe activitatea curentă care la apelat. Controlul are implementat minim 3 evenimente (START, PROGRESS[%], STOP) Testarea si prezentarea implementării va fi efectuată în cadrul unui TASK ASYNC cu un delay de execuție de maxim 5-10s.

Interfața grafică a aplicației

MainActivity package com.danrotaru.pamlab4; import androidx.appcompat.app.AppCompatActivity; import import import import import import import import

android.annotation.SuppressLint; android.graphics.drawable.AnimationDrawable; android.os.Bundle; android.widget.Button; android.widget.ImageView; android.widget.ProgressBar; android.widget.RadioButton; android.widget.TextView;

public class MainActivity extends AppCompatActivity { AnimationDrawable animation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

ImageView image = (ImageView) findViewById(R.id.image); image.setBackgroundResource(R.drawable.animation); animation = (AnimationDrawable) image.getBackground(); ProgressBar progress = (ProgressBar) findViewById(R.id.progress); newDrawable(); final int[] finalTotalTime = {duration()};

Button pause = (Button) findViewById(R.id.pause); pause.setOnClickListener(v -> { animation.stop(); progress.clearAnimation(); progress.setProgress(0); }); Button play = (Button) findViewById(R.id.play); play.setOnClickListener(v -> { animation.stop(); animation.start();

ProgressBarAnimation anim = new ProgressBarAnimation(progress, 0, 100); anim.setDuration(finalTotalTime[0]); progress.startAnimation(anim); });

final RadioButton[] check1 = {findViewById(R.id.anim1)}; RadioButton check2 = findViewById(R.id.anim2); RadioButton check3 = findViewById(R.id.anim3); check1[0].setOnClickListener(v -> { check2.setChecked(false); check3.setChecked(false); image.setBackgroundResource(R.drawable.animation); animation = (AnimationDrawable) image.getBackground(); progress.clearAnimation(); progress.setProgress(0); finalTotalTime[0] = duration(); newDrawable(); }); check2.setOnClickListener(v -> { check1[0].setChecked(false); check3.setChecked(false); image.setBackgroundResource(R.drawable.animation1); animation = (AnimationDrawable) image.getBackground(); progress.clearAnimation(); progress.setProgress(0); finalTotalTime[0] = duration(); newDrawable(); }); check3.setOnClickListener(v -> { check2.setChecked(false); check1[0].setChecked(false); image.setBackgroundResource(R.drawable.animation2); animation = (AnimationDrawable) image.getBackground(); progress.clearAnimation(); progress.setProgress(0); finalTotalTime[0] = duration(); newDrawable(); });

} public int duration(){ int totalTime = 0; for(int i = 0; i < animation.getNumberOfFrames(); i++){

totalTime += animation.getDuration(i); } return totalTime; } @SuppressLint("SetTextI18n") public void newDrawable(){ int numberOfFrames = animation.getNumberOfFrames(); int totalTime = duration();

int x = totalTime / numberOfFrames; TextView t = findViewById(R.id.textView); t.setText("Animation Frames: " + numberOfFrames + "\nTotal duration: "+totalTime+"\nEach frame time: " + x + "ms"); } }

ProgressBarAnimation package com.danrotaru.pamlab4; import import import import

android.view.animation.Animation; android.view.animation.Transformation; android.widget.ProgressBar; android.widget.TextView;

public class ProgressBarAnimation extends Animation { private ProgressBar progressBar; private float from; private float to; public ProgressBarAnimation(ProgressBar progressBar, float from, float to) { super(); this.progressBar = progressBar; this.from = from; this.to = to; } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { super.applyTransformation(interpolatedTime, t); float value = from + (to - from) * interpolatedTime; progressBar.setProgress((int) value); } }