Grafikalıq fayl resursları
En’ ken’ tarqalg’an resurs dereklerinen biri bul grafikaliq fayl esaplanadi. Android to’mendegi fayl formatlarin qollap-quwatlaydi: .png (abzal), .jpg (qabil etiledi), .gif (abzal emes). Joybardag’i grafikaliq fayllar ushin a’dette res/drawable papkasi a’lleqashan jaratilg’an boladi. A’dette, ol a’lleqashan bir qatar fayllardi o'z ishine aladi - bir neshe piktogramma fayllari:
Usi papkag’a grafikaliq fayllardi jaylastirg’animizda Android olardin’ ha’r biri ushin Drawable resursin jaratadi. Bundan son’ biz Java kodinda to’mendegishe resursqa mu’rajat etiwimiz mu’mkin:
1
2
|
"vertical-align: inherit;">"vertical-align: inherit;">R.drawable.fayl_ati
|
Ya’ki xml kodinda:
1
2
|
@[paket_ati:]drawable/fayl_ati
|
Misali, res/drawable papkasina joybarg’a bir neshe grafikaliq fayldi qosamiz. Buni a’melge asiriw ushin qatti diskimizge qandayda bir png yaki jpg ken’eytpege iye fayldan nusxa alamiz ha’m oni res/drawable papkasina jaylastiramiz (joybarg’a nusxalaw ushin a’piwayi Copy-paste dan paydalaniladi).
Keyinshelik bizden drawable ya’ki drawable-24 papkalarinan birin tan’law soraladi. A’piwayi grafikaliq fayllardi qosiw ushin drawable di tan’laymiz:
Bul jerde da’rhal grafikaliq fayldin’ qosimshag’a qosiliwi ha’m sonin’ menen onin’ ko’lemin asiriwin esapqa aliw kerek. Bunnan tisqari, u’lken ko’lemge iye fayllar islewge keri ta'sir ko'rsetedi. Sonliqtan kishi ha’m optimallastirilg’an (qisilg’an) grafikaliq fayllardan paydalaniw jaqsiraq. Sonida aytip o’tiw kerek, usi papkag’a qosilg’an barliq grafikaliq fayllar joybardi jaratiw waqtinda aapt utiliti ja’rdeminde avtomatikaliq tu’rde optimallastiriliwi mu’mkin. Bul sipatin jog’altpastan fayl ko’lemin kemeytiw imkanin beredi.
Fayldi nusxalawda bizden og’an jan’a at ornatiw usiniladi.
Siz fayl atin o'zgertiriwin’iz ya’ki oni alding’idan qaldiriwin’iz mu’mkin. Bizde fayl dubi2.png dep ataladi. Keyin Refactor tu’ymesini basamiz. Sonnan son’, tan’lang’an grafikaliq fayl drawable papkasina qosiladi .
Androidda grafikaliq fayllar menen islew ushin tu’rli elementlerden paydalaniwin’iz mu’mkin, biraq ImageView su’wretlerdi tuwridan-tuwri ko'rsetiw ushin arnalg’an. Solay etip, biz activity_main.xml faylin to’mendegishe o'zgertiremiz:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/dubi2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
|
Bunday jag’dayda fayldi ImageView da ko'rsetiw ushin elementte android:src atributi ornatiladi . Onin’ a’hmiyeti sonda, ol ken’eytpesiz fayl ati menen bir qiyli bolg’an grafikaliq resurs atin belgileydi. Son’, Android Studioda Preview ya’ki dizayner rejiminde siz su’wretti qosimshani iske tu’sirgenin’izde ko'riwin’iz mu’mkin:
Eger biz Java kodinda ImageView di jaratqan bolsaq ha’m kodtan resursti qollasaq, onday jag’dayda activity to’mendegishe ko'riniwi mu’mkin:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package com.example.viewapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
ConstraintLayout constraintLayout = new ConstraintLayout(this);
ImageView imageView = new ImageView(this);
//
imageView.setImageResource(R.drawable.dubi2);
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams
(ConstraintLayout.LayoutParams.WRAP_CONTENT , ConstraintLayout.LayoutParams.WRAP_CONTENT);
layoutParams.leftToLeft = ConstraintLayout.LayoutParams.PARENT_ID;
layoutParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
imageView.setLayoutParams(layoutParams);
constraintLayout.addView(imageView);
setContentView(constraintLayout);
}
}
|
Bunday jag’dayda, drawable resursi tuwridan-tuwri imageView.setImageResource() metodina uzatiladi ha’m ko’rinis sol ta’rizde ornatiladi. Na’tiyjede bizde usinday na’tiyjege erisemiz.
1
|
imageView.setImageResource(R.drawable.dubi2);
|
Ayirim jag’daylarda, paydalaniwdan aldin resursti qandayda bir tu’rde qayta islew ya’ki oni basqa stsenariylarda isletiw kerek boliwi mu’mkin. Bunday jag’dayda, biz aldin oni Drawable ob'ekti sipatinda aliwimiz ha’m keyin oni ma’selelerimiz ushin isletiwimiz mu’mkin:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package com.example.viewapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.res.ResourcesCompat;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
ConstraintLayout constraintLayout = new ConstraintLayout(this);
ImageView imageView = new ImageView(this);
Resources res = getResources();
Drawable drawable = ResourcesCompat.getDrawable(res, R.drawable.dubi2, null);
//
imageView.setImageDrawable(drawable);
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams
(ConstraintLayout.LayoutParams.WRAP_CONTENT , ConstraintLayout.LayoutParams.WRAP_CONTENT);
layoutParams.leftToLeft = ConstraintLayout.LayoutParams.PARENT_ID;
layoutParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
imageView.setLayoutParams(layoutParams);
constraintLayout.addView(imageView);
setContentView(constraintLayout);
}
}
|
Resursti aliw ushin ResourcesCompat.getDrawable () usulinan paydalaniladi, ol Resources obyekti, resurs identifikatori ha’m tema arqali uzatiladi. Bunday jag’dayda, tema biz ushin a’hmiyetli emes, sonliqtan biz ol ushin null ma’nisin o'tkizemiz. Resurs Drawable obiekti sipatinda qaytariladi:
1
|
Drawable drawable = ResourcesCompat.getDrawable(res, R.drawable.dubi2, null);
|
Keyin, ma’selen, siz resursti ImageView obiektine setImageDrawable ( ) usuli arqalida o'tkiziwin’iz mu’mkin.
1
|
imageView.setImageDrawable(рисуемый);
|
Do'stlaringiz bilan baham: |