2020년 5월 29일 금요일

android progress bar 원형으로 setProgress 설정


progress bar ?

어떠한 작업을 수행할 시 기다려야 하는 하나의 표시로서 나타낼 수 있고, 퍼센트지로 작업 완료시점을 알릴 수 있습니다.


저는 XML을 통해서가 아닌 자바코드를 사용해서 레이아웃에 추가해보도록 하겠습니다.


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.activity_main);   
    ProgressBar pg = new ProgressBar(getApplicationContext(),null       
     ,android.R.attr.progressBarStyleLarge);  
    ViewGroup layout = findViewById(R.id.linearLayout);   
    layout.addView(pg);
}


이렇게 하면 뺑글뺑글 돌아갑니다.
이렇게 선언 하시면 가장 기본적인 모션으로 돌아가는 것 같습니다.
@Overrideprotected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    ProgressBar pg = new ProgressBar(getApplicationContext(),null            ,android.R.attr.progressBarStyleLarge);
    pg.setMax(100);
    pg.setProgress(50);
    LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);    layout.addView(pg);}
Max와 Progress 를 설정했지만 위 영상과 같이 열심히 돌아기만 합니다.

하지만 돌아가는 모션이 아닌 상황에 따라 달라지게 할 수 없을까요 ?
setProgress 설정과 똑같이 적용될 수 있도록 말이죠. 



protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);   
 ProgressBar pg = new ProgressBar(getApplicationContext(),null       
     ,android.R.attr.progressBarStyleHorizontal);  
 // pg.set    ViewGroup layout = findViewById(R.id.linearLayout);   
 layout.addView(pg);}

Large -> Horizontal 로 변경했습니다. 
이렇게 하면 setProgress 설정이 반영되어 채워지게 됩니다.
하지만!! 이렇게 하면 원형이 아닌 일직선으로 스타일이 변경이 됩니다
원으로 하고싶은데..;;



drawable 폴더에 이름의 자유롭게 xml 파일로 하나 만들어주세요
저는 progressbar_ring으로 만들었습니다.

<shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="ring"    android:innerRadiusRatio="2.5"    android:thickness="5dp"    android:useLevel="true">    <solid android:color ="@color/colorAccent"></solid>
</shape>

shape = "ring" 과 함께
링 비율과 링을 이루는 두께를 설정합니다.

@Overrideprotected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    ProgressBar pg = new ProgressBar(getApplicationContext(),null            ,android.R.attr.progressBarStyleHorizontal);    pg.setProgressDrawable(getResources().getDrawable(R.drawable.progressbar_ring,null));    pg.setProgress(100);    pg.setProgress(50);    LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);    layout.addView(pg);}

Drawable 폴더에 생성한 xml 파일을 설정해주세요. 그리고 이대로 실행하면 화면이 짤려 보일거에요.
그래서 Layout 설정을 했습니다
짤려보이신다면 아래와 같이 설정해주세요 

@Overrideprotected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    ProgressBar pg = new ProgressBar(getApplicationContext(),null            ,android.R.attr.progressBarStyleHorizontal);    pg.setProgressDrawable(getResources().getDrawable(R.drawable.progressbar_ring,null));
    LinearLayout.LayoutParams params =new LinearLayout.LayoutParams(200,200);
    pg.setLayoutParams(params);
    pg.setProgress(100);    pg.setProgress(50);    LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);    layout.addView(pg);}

LinearLayout.LayoutParams(200,200);

200,200 은 폭과 넓이를 의미합니다 

참고 : https://www.youtube.com/watch?v=FCScsD0ZzOg
https://www.youtube.com/watch?v=hSfN_aYKkzo

댓글 없음

댓글 쓰기

© 특히하고 특별한
Maira Gall