一个简单的图片浏览器

Java代码

package com.android.myandroid;
import android.R.integer;




import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;




public class MainActivity extends Activity {
    //定义一个访问图片的数组
int[] images = new int []
{R.drawable.de8,R.drawable.de9,R.drawable.de10,R.drawable.de11,R.drawable.de12,R.drawable.de13,
R.drawable.de14,R.drawable.de15,R.drawable.de16,R.drawable.de17,R.drawable.de18,R.drawable.de19,
R.drawable.de20,R.drawable.de21,R.drawable.de22,R.drawable.de23,R.drawable.de24,R.drawable.de25,R.drawable.de27,
R.drawable.de27
};
  int currentimg = 2;
  private int alpha = 255;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button plus = (Button)findViewById(R.id.button1);
        final Button minus = (Button)findViewById(R.id.button2);
         final Button next = (Button)findViewById(R.id.button3);
        final ImageView image1 = (ImageView)findViewById(R.id.imageView1);
        final ImageView image2 = (ImageView)findViewById(R.id.imageView2);
        image1.setImageResource(images[2]);
        next.setOnClickListener(new OnClickListener()
        {
        public void onClick(View v){
        Toast toast =Toast.makeText(MainActivity.this, "少女时代!", 3000);
        toast.show();
        image1.setImageResource(images[++currentimg%images.length]);
        }
        });
        //定义改变透明度、alpha是图片的透明度 取值在0--250之间、数值越大透明度越高
        OnClickListener listener = new OnClickListener(){
        public void onClick(View v){
        if(v==plus){
        alpha+=20;
        }
        if(v==minus)
        {alpha-=20;}
        if(alpha>=255){
        alpha=255;}
        if(alpha<=0){
        alpha=0;}
        image1.setAlpha(alpha);
        }
        };
        //为两个按钮绑定监听器
        plus.setOnClickListener(listener);
        minus.setOnClickListener(listener);
        image1.setOnTouchListener(new OnTouchListener()
        {
           
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
BitmapDrawable bitmapDrawable = (BitmapDrawable)image1.getDrawable();//设置截图来源
//获取第一个图片显示框的位图
Bitmap bitmap = bitmapDrawable.getBitmap();
//bitmap实际大小与第一个ImageView的缩放比例
double scale = bitmap.getWidth()/320.0;
//获取显示图片的开始点
   int x = (int)((event).getX()*scale);
int y = (int)((event).getY()*scale);
if(x+120>bitmap.getWidth()){
x=bitmap.getWidth()-120;
}
if(y+120>bitmap.getHeight()){
y=bitmap.getHeight()-120;
}
image2.setImageBitmap(Bitmap.createBitmap(bitmap,x,y,120,120));
image2.setAlpha(alpha);
return false;
}
       
        });
        
    }
}

Xml文件

<LinearLayout 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:orientation="vertical"
    tools:context="${packageName}.${activityClass}" >


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >


        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="增加透明度" />


        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="降低透明度" />
     
          <Button
            android:id="@+id/button3"
          
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="下一张" />






   
    </LinearLayout>


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="240dp"
        android:gravity="center" />


    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="3dp"
        android:background="#0f0" />


</LinearLayout>

效果图



好的,以下是一个简单的Python图片浏览器的代码示例: ```python from tkinter import * from PIL import ImageTk, Image import os # 设置窗口和图片大小 WINDOW_SIZE = "500x500" IMG_SIZE = (400, 400) # 获取当前文件夹下的所有图片 def get_image_list(): image_list = [] for file in os.listdir(): if file.endswith(".jpg") or file.endswith(".png"): image_list.append(file) return image_list # 显示图片 def show_image(image_path): img = Image.open(image_path) img = img.resize(IMG_SIZE, Image.ANTIALIAS) photo = ImageTk.PhotoImage(img) label = Label(image=photo) label.image = photo label.place(x=50, y=50) # 切换图片 def next_image(): global image_index image_index += 1 if image_index >= len(image_list): image_index = 0 show_image(image_list[image_index]) def previous_image(): global image_index image_index -= 1 if image_index < 0: image_index = len(image_list) - 1 show_image(image_list[image_index]) # 初始化窗口 root = Tk() root.geometry(WINDOW_SIZE) root.title("Image Viewer") # 获取图片列表并显示第一张图片 image_list = get_image_list() image_index = 0 if image_list: show_image(image_list[0]) # 添加按钮 previous_button = Button(root, text="Previous", command=previous_image) previous_button.place(x=50, y=450) next_button = Button(root, text="Next", command=next_image) next_button.place(x=400, y=450) # 运行窗口 root.mainloop() ``` 该代码使用了`tkinter`和`PIL`库来创建窗口并显示图片。首先通过`get_image_list()`函数获取当前文件夹下所有的图片文件,然后通过`show_image()`函数来显示图片。`next_image()`和`previous_image()`函数用于切换图片。 在初始化窗口之后,我们获取了图片列表并显示第一张图片。之后,我们添加了两个按钮,`previous_button`和`next_button`,并将它们绑定到对应的函数。最后,我们运行窗口并进入主循环,等待用户的操作。 请注意,该代码只是一个简化的示例,可能需要根据具体需求进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值