cuốn sách gpt4 ai đã làm

Android - Giảm kích thước nút một cách linh hoạt khi nhấn và khôi phục kích thước của nút khi nhả

In lại Tác giả: Taklimakan Thời gian cập nhật: 2023-11-02 20:28:41 26 4
mua khóa gpt4 Nike

Tôi muốn tạo một nút thay đổi kích thước (nhỏ hơn một chút) khi nhấn và sau khi nhả nút lại, kích thước sẽ thay đổi về kích thước bình thường. Tôi đang sử dụng Scal xml để đạt được điều này, nhưng nó tự định vị lại ngay cả khi tôi không nhả nút.

Ở đây tôi đang đề cập đến nút xem hình ảnh.

Đây là mã nguồn của tôi: -

imgSpin = (ImageView) findViewById(R.id.iv_spins);
imgSpin.setOnTouchListener(Chế độ xem mới.OnTouchListener() {
@Ghi đè
public boolean onTouch(Xem v, sự kiện MotionEvent) {
chuyển đổi (event.getAction()) {
trường hợp MotionEvent.ACTION_DOWN:
imgSpin.startAnimation(mAnimation);
khe quay();
phá vỡ;

trường hợp MotionEvent.ACTION_UP:
imgSpin.clearAnimation();
imgSpin.setEnabled(false);
phá vỡ;
}
trả về đúng sự thật;
}
});

ScalXml của tôi: -


android:thời lượng="200"
android:fromXScale=".8"
android:fromYScale=".8"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0" >


XML của tôi: -

 
android:id="@+id/layout_status"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".2"
android:orientation="ngang" >

<>
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5" />


android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2.1"
android:background="@drawable/bootser" />


android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2.5"
android:background="@drawable/bet_bg"
android:trọng lực="center_vertical"
android:orientation="ngang"
android:weightSum="1" >


android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_rabity="center_vertical"
android:layout_weight=".2"
android:src="@drawable/leftarrow" />


android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:layout_weight=".6"
android:trọng lực="trung tâm"
android:text="Cá cược"
android:textColor="@android:color/white" />


android:layout_width="0dp"
android:layout_height="40dp"
android:layout_rabity="center_vertical"
android:layout_weight=".2"
android:src="@drawable/rightarrow" />



android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2.5"
android:background="@drawable/container"
android:trọng lực="trung tâm" >


android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="chiến thắng"
android:textColor="@android:color/white" />



android:id="@+id/iv_spins"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.9"
android:background="@drawable/spin" />

<>
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5" />

Tôi đã thử sử dụng paramLayout, nhưng vì tôi đang đặt trọng số trong XML nên nó không tạo hiệu ứng cho tôi.

Tôi đã sử dụng phương pháp này: -

trường hợp MotionEvent.ACTION_DOWN:
Thông số ViewGroup.LayoutParams = imgSpin.getLayoutParams();
// Chiều rộng mới của nút
params.width = params.width - (params.width *90/100);

imgSpin.setLayoutParams(params);

phá vỡ;

params.width luôn trả về 0 Vì vậy, tôi đã cố gắng thực hiện điều này với trọng lượng, bằng cách sử dụng trọng lượng, Chế độ xem của tôi bị trục trặc khi nhấp chuột.. Tôi muốn nó mượt mà nên tôi thích thu phóng hơn

Phương pháp thứ hai: -

trường hợp MotionEvent.ACTION_DOWN:
Thông số LinearLayout.LayoutParams = (android.widget.LinearLayout.LayoutParams) imgSpin
.getLayoutParams();
// Chiều rộng mới của nút
thông số.trọng lượng = 1,7f;

imgSpin.setLayoutParams(params);
// imgSpin.startAnimation(mAnimation);
// spinslot();
phá vỡ;

trường hợp MotionEvent.ACTION_UP:
LinearLayout.LayoutParams params2 = (android.widget.LinearLayout.LayoutParams) imgSpin
.getLayoutParams();
// Chiều rộng mới của nút
params2.weight = 1,9f;

imgSpin.setLayoutParams(params2);
// imgSpin.clearAnimation();
imgSpin.setEnabled(false);
phá vỡ;
}
trả về đúng sự thật;

Được nhưng không mượt

xin vui lòng cho tôi biết làm thế nào để làm điều đó

Cảm ơn

câu trả lời hay nhất

Đây là những gì bạn muốn: -

imgSpin.setOnTouchListener(Chế độ xem mới.OnTouchListener() {
@Ghi đè
public boolean onTouch(Xem v, sự kiện MotionEvent) {
chuyển đổi (event.getAction()) {
trường hợp MotionEvent.ACTION_DOWN:
ObjectAnimatorscaleDownX = ObjectAnimator.ofFloat(imgSpin,
"scaleX", 0,8f);
ObjectAnimatorscaleDownY = ObjectAnimator.ofFloat(imgSpin,
"tỷ lệY", 0,8f);
scaleDownX.setDuration(1000);
scaleDownY.setDuration(1000);

AnimatorSetscaleDown = new AnimatorSet();
scaleDown.play(scaleDownX).with(scaleDownY);

quy môDown.start();

khe quay();
phá vỡ;

trường hợp MotionEvent.ACTION_UP:
ObjectAnimatorscaleDownX2 = ObjectAnimator.ofFloat(
imgSpin, "scaleX", 1f);
Thang đo ObjectAnimatorDownY2 = ObjectAnimator.ofFloat(
imgSpin, "scaleY", 1f);
scaleDownX2.setDuration(1000);
scaleDownY2.setDuration(1000);

AnimatorSetscaleDown2 = new AnimatorSet();
scaleDown2.play(scaleDownX2).with(scaleDownY2);

quy môDown2.start();

imgSpin.setEnabled(false);
phá vỡ;
}
trả về đúng sự thật;
}
});

Chỉ cần thực hiện một số thay đổi sau trong xml của bạn: -


android:id="@+id/iv_spins"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.9"
android:justViewBounds="true"
android:background="@drawable/spin"
android:scaleX="1"
android:scaleY="1" />

sử dụngscaleDown.play(scaleDownX).with(scaleDownY), nó sẽ giữ nguyên vị trí của ảnh động và không trở về vị trí ban đầu

Hy vọng điều này sẽ giải quyết được vấn đề của bạn, chúc mừng

Về android - Giảm kích thước nút một cách linh hoạt khi nhấn và khôi phục kích thước của nó khi phát hành, chúng tôi đã tìm thấy một câu hỏi tương tự trên Stack Overflow: https://stackoverflow.com/questions/37318133/

26 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress