Thứ Tư, 3 tháng 10, 2018

Android - Trình chuyển đổi hình ảnh

Học lập trình Android. Đôi khi bạn không muốn một hình ảnh xuất hiện đột ngột trên màn hình, thay vào đó bạn muốn áp dụng một số loại hoạt ảnh cho hình ảnh khi nó chuyển đổi từ hình này sang hình khác. Điều này được hỗ trợ bởi Android dưới dạng ImageSwitcher.

Trình chuyển đổi hình ảnh cho phép bạn thêm một số hiệu ứng chuyển tiếp trên hình ảnh thông qua cách chúng xuất hiện trên màn hình. Để sử dụng Trình chuyển đổi hình ảnh, trước tiên bạn cần định nghĩa thành phần XML của nó. Cú pháp của nó được đưa ra dưới đây
<ImageSwitcher
   android:id="@+id/imageSwitcher1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerHorizontal="true"
   android:layout_centerVertical="true" >
</ImageSwitcher>
Bây giờ chúng ta tạo ra một ý định của ImageSwithcer trong tệp java và nhận được một tham chiếu đến thành phần XML này. Cú pháp của nó được đưa ra dưới đây

private ImageSwitcher imageSwitcher;
imageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher1);

Điều tiếp theo chúng ta cần thực hiện giao diện ViewFactory và thực hiện phương thức chưa thực hiện trả về một imageView. Cú pháp của nó là dưới đây
imageSwitcher.setImageResource(R.drawable.ic_launcher);
imageSwitcher.setFactory(new ViewFactory() {
   public View makeView() {
      ImageView myView = new ImageView(getApplicationContext());
      return myView;
   }
}

Điều cuối cùng bạn cần làm là thêm Animation vào ImageSwitcher.

Bạn cần định nghĩa một đối tượng của lớp Animation thông qua lớp AnimationUtilities bằng cách gọi phương thức tĩnh loadAnimation. Cú pháp của nó được đưa ra dưới đây
Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
imageSwitcher.setInAnimation(in);
imageSwitcher.setOutAnimation(out);         

Phương thức setInAnimaton đặt hoạt ảnh của sự xuất hiện của đối tượng trên màn hình trong khi setOutAnimation làm ngược lại. Phương thức loadAnimation () tạo ra một đối tượng hoạt hình.

Ngoài các phương thức này, còn có các phương thức khác được định nghĩa trong lớp ImageSwitcher. Chúng được định nghĩa bên dưới

Sr.NoPhương pháp & mô tả
1setImageDrawable (Drawable drawable)

Đặt hình ảnh bằng trình chuyển đổi hình ảnh. Hình ảnh được truyền dưới dạng bitmap
2setImageResource (int resid)

Đặt hình ảnh bằng trình chuyển đổi hình ảnh. Hình ảnh được chuyển dưới dạng id nguyên
3setImageURI (Uri uri)

Đặt hình ảnh bằng trình chuyển đổi hình ảnh. Hình ảnh này được truyền dưới dạng URI
4ImageSwitcher (Ngữ cảnh bối cảnh, AttributeSet attrs)

Trả về đối tượng trình chuyển đổi hình ảnh đã thiết lập một số thuộc tính được truyền trong phương thức
5onInitializeAccessibilityEvent (AccessibilityEvent event)

Khởi tạo một AccessibilityEvent với thông tin về Chế độ xem này là nguồn sự kiện
6onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)

Khởi tạo một AccessibilityNodeInfo với thông tin về khung nhìn này

Thí dụ

Ví dụ dưới đây minh họa một số hiệu ứng của trình chuyển đổi hình ảnh trên bitmap. Nó thùng một ứng dụng cơ bản cho phép bạn xem các hiệu ứng hình ảnh động trên hình ảnh.

Để thử nghiệm với ví dụ này, bạn cần chạy trên một thiết bị thực tế.

Các bướcSự miêu tả
1Bạn sẽ sử dụng Android studio IDE để tạo ứng dụng Android theo gói com.example.sairamkrishna.myapplication.
2Sửa đổi tệp src / MainActivity.java để thêm mã cần thiết.
3Sửa đổi res / layout / activity_main để thêm các thành phần XML tương ứng
4Chạy ứng dụng và chọn thiết bị Android đang chạy và cài đặt ứng dụng trên đó và xác minh kết quả
Sau đây là nội dung của tệp hoạt động chính đã sửa đổi src / MainActivity.java .
Trong mã dưới đây tp và abc biểu thị logo của tutorialspoint.com
package com.example.sairamkrishna.myapplication;

import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.os.Bundle;
import android.view.View;

import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity {
   private ImageSwitcher sw;
   private Button b1,b2;

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

      b1 = (Button) findViewById(R.id.button);
      b2 = (Button) findViewById(R.id.button2);

      sw = (ImageSwitcher) findViewById(R.id.imageSwitcher);
      sw.setFactory(new ViewFactory() {
         @Override
         public View makeView() {
            ImageView myView = new ImageView(getApplicationContext());
            myView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            myView.setLayoutParams(new 
               ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,
                  LayoutParams.WRAP_CONTENT));
            return myView;
         }
      });

      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "previous Image",
               Toast.LENGTH_LONG).show();
            sw.setImageResource(R.drawable.abc);
         }
      });

      b2.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Next Image",
               Toast.LENGTH_LONG).show();
            sw.setImageResource(R.drawable.tp);
         }
      });
   }
}
Sau đây là nội dung sửa đổi của xml 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:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" 
   tools:context=".MainActivity">
      
   <TextView android:text="Gestures  Example" 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textview"
      android:textSize="35dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials point"
      android:id="@+id/textView"
      android:layout_below="@+id/textview"
      android:layout_centerHorizontal="true"
      android:textColor="#ff7aff24"
      android:textSize="35dp" />
      
   <ImageSwitcher
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageSwitcher"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="168dp" />
      
   <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/left"
        android:id="@+id/button"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true" />

   <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/right"
        android:id="@+id/button2"
        android:layout_alignParentBottom="true"
        android:layout_alignLeft="@+id/button"
        android:layout_alignStart="@+id/button" />
      
</RelativeLayout>
Sau đây là nội dung của tệp Strings.xml .
<resources>
    <string name="app_name">My Application</string>
    <string name="left"><![CDATA[<]]></string>
    <string name="right"><![CDATA[>]]></string>
</resources>
Sau đây là nội dung của tệp AndroidManifest.xml .
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sairamkrishna.myapplication"

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.sairamkrishna.myapplication.MainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      
   </application>
</manifest>
Hãy thử chạy ứng dụng của bạn, chúng tôi vừa sửa đổi. Tôi cho rằng bạn đã tạo AVD của mình trong khi thiết lập môi trường.

Để chạy ứng dụng từ Android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Chạy từ thanh công cụ.

Android studio cài đặt ứng dụng trên AVD của bạn và khởi động ứng dụng và nếu mọi thứ đều ổn với thiết lập và ứng dụng của bạn, ứng dụng sẽ hiển thị cửa sổ Trình mô phỏng sau

Học lập trình Android

Bây giờ nếu bạn sẽ nhìn vào màn hình thiết bị của bạn, bạn sẽ thấy hai nút.

Bây giờ chỉ cần chọn nút trên bên phải mũi tên. Một hình ảnh sẽ xuất hiện từ phải và di chuyển về phía bên trái. Nó được hiển thị dưới đây

Học lập trình Android
Bây giờ hãy nhấn vào nút bên dưới, điều này sẽ mang lại hình ảnh trước đó với một số chuyển đổi. Nó được hiển thị dưới đây

Không có nhận xét nào:

Đăng nhận xét

Lập trình Android - RenderScript

Trong chương này, chúng ta sẽ tìm hiểu về Android RenderScript. Thông thường các ứng dụng trên Android được thiết kế để tiêu thụ tài nguyên ...