قالب و افزونه وردپرس

imageView در اندروید و کار با عکس ها در اندروید

منتشرشده توسط حامد قنبری در تاریخ

برای افزودن عکس به صفحه اکتیویتی از دو روش استفاده می کنیم :

روش اول از طریق  لایوت xml:

در این روش لایه موردنظرمان را از پوشه res/ layout / activity_main  باز می کنیم ودر قسمت مربوط به لایه گرافیکی (design) ، از نوار ابزارهای برنامه ، آیکن  ونوشته مربوط به ImageView  را پیدا کرده و به داخل اکتیویتی دراگ می کنیم.

 

یا اینکه  که از همان مسیر ذکر شده در قسمت مربوط به کد نویسی (text)  کدهای زیر را  به شکل  زیر در آن قرار می دهیم:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.psrd.myapplication.MainActivity"
    android:background="#e3d7d7">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
    />
</RelativeLayout>

روش سوم از طریق برنامه نویسی در کلاس جاوا است :

ImageView imageview = new ImageView(MainActivity.this);
RelativeLayout relativelayout = (RelativeLayout)findViewById(R.id.layout1);
LinearLayout.LayoutParams params = new LinearLayout
        .LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

// drawableاضافه کردن عکس از مسیر 
imageview.setImageResource(R.drawable.tel);
imageview.setLayoutParams(params);
relativelayout.addView(imageview);

به منظور درک  بهتر هر دو آموزش، با تلفیق  هر دو موضوع مثالی را برایتان مطرح می کنیم.

گر در داخل برنامه اکلیپس یا اندروید استودیو قرار دارید پروژه جدیدی را  ایجاد کنید

اکلیپس :File ⇒ New Android ⇒ Application Project

اندروید استودیو :File ⇒ New  ⇒ New Project

اما اگر هنوز برنامه خود را باز نکرده اید یکی از برنامه های فوق را باز نموده و بعد از تعیین مشخصات(نامگذاری) ، تعیین حداقل sdk  و نوع اکتیویتی (blank  یا Empty) ، نام اکتیویتی ابتدایی و اصلی خود  را همان MainActivity قرار دهید. بعد از لود کامل برنامه ، در مسیر res ⇒layout لایه متناظر اکتیوتی اصلی یعنی activity_main را پیدا نمود و کدهای مندرج و پیش فرض آن را پاک کرده و کدهای xml  زیر را به آن اضافه کنید:

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.psrd.myapplication.MainActivity"
    android:background="#8ea8ca"
    android:id="@+id/layout1">


    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="118dp" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:text="ایجاد imageView از طریق برنامه نویسی در کلاس "
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ایجاد imageView  در لایوت xml"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="29dp" />
</RelativeLayout>

ضروری است که دو عکس با نام های متفاوت و با فرمت مجاز در فایل drawable  قرار داده شود.res/drawable

سپس کلاس MainActivity  را باز نموده و کده های زیر را در آن قرار دهید:

p
package com.example.psrd.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {


    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


       img = (ImageView)findViewById(R.id.imageView);
        img.setImageResource(R.drawable.tel);

        Button btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                img.setVisibility(View.INVISIBLE);
                ImageView imageview = new ImageView(MainActivity.this);
                RelativeLayout relativelayout = (RelativeLayout) findViewById(R.id.layout1);
                LinearLayout.LayoutParams params = new LinearLayout
                        .LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

                // اضافه کردن عکس از مسیر drawable
                imageview.setImageResource(R.drawable.img2);
                imageview.setLayoutParams(params);
                relativelayout.addView(imageview);
            }
        });




    }
}

 

حال می توانید پروژه خودتان را اجرا کنید و نتیجه کارتان را ببینید.

 


1 دیدگاه

بهروز · دسامبر 3, 2018 در 7:56 ق.ظ

با سلام ببخشید ایا میشه به عکس یک لینک داد ؟

پاسخ دادن به Prestamos inmediatos لغو پاسخ

Avatar placeholder

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *