mirror of https://github.com/axmolengine/axmol.git
Webview opacity and background transparency (#17831)
* Added three new methods for UiWebView. SetOpacityWebView(float), getOpacityWebView(), setBackgroundTransparent() in IOS * Implemented methods for UiWebView. SetOpacityWebView(float), getOpacityWebView(). They only work using android sdk +11. * New methods not implemented for Tizen. * Added new button for testing the opacity in the cocos2d-x test application. * Added setBackgroundTransparent to android, and his UI-Test. * Added setBackgroundTransparent to android, and his UI-Test. * Added keyword const to the getters. * Android sdk reflection. +11sdk for chaging the opacity. * Reflection used for the webview.setLayerType, webview.setAlpha, webview.getAlpha * Unified exceptions.
This commit is contained in:
parent
421bd16cb7
commit
72ad341d24
|
@ -1,12 +1,19 @@
|
||||||
package org.cocos2dx.lib;
|
package org.cocos2dx.lib;
|
||||||
|
|
||||||
|
import android.annotation.TargetApi;
|
||||||
|
import android.graphics.Color;
|
||||||
|
import android.graphics.Paint;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
|
import android.util.Log;
|
||||||
import android.util.SparseArray;
|
import android.util.SparseArray;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.webkit.WebView;
|
||||||
import android.widget.FrameLayout;
|
import android.widget.FrameLayout;
|
||||||
import android.webkit.WebSettings;
|
import android.webkit.WebSettings;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.FutureTask;
|
import java.util.concurrent.FutureTask;
|
||||||
|
@ -95,6 +102,75 @@ public class Cocos2dxWebViewHelper {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void setBackgroundTransparent(final int index) {
|
||||||
|
if(android.os.Build.VERSION.SDK_INT >10) {
|
||||||
|
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Cocos2dxWebView webView = webViews.get(index);
|
||||||
|
if (webView != null) {
|
||||||
|
webView.setBackgroundColor(Color.TRANSPARENT);
|
||||||
|
try {
|
||||||
|
Method method = webView.getClass().getMethod("setLayerType",int.class,Paint.class);
|
||||||
|
method.invoke(webView,WebView.LAYER_TYPE_SOFTWARE,null);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setOpacityWebView(final int index, final float opacity) {
|
||||||
|
if(android.os.Build.VERSION.SDK_INT >10){
|
||||||
|
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Cocos2dxWebView webView = webViews.get(index);
|
||||||
|
if (webView != null) {
|
||||||
|
try {
|
||||||
|
Method method = webView.getClass().getMethod("setAlpha",float.class);
|
||||||
|
method.invoke(webView,opacity);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static float getOpacityWebView(final int index) {
|
||||||
|
if(android.os.Build.VERSION.SDK_INT >10){
|
||||||
|
FutureTask<Float> futureResult = new FutureTask<Float>(new Callable<Float>() {
|
||||||
|
@Override
|
||||||
|
public Float call() throws Exception {
|
||||||
|
float opacity=0.f;
|
||||||
|
Cocos2dxWebView webView = webViews.get(index);
|
||||||
|
Object valueToReturn=null;
|
||||||
|
if (webView != null) {
|
||||||
|
try {
|
||||||
|
Method method = webView.getClass().getMethod("getAlpha");
|
||||||
|
valueToReturn = method.invoke(webView);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (Float) valueToReturn;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
sCocos2dxActivity.runOnUiThread(futureResult);
|
||||||
|
try {
|
||||||
|
return futureResult.get();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
public static void setWebViewRect(final int index, final int left, final int top, final int maxWidth, final int maxHeight) {
|
public static void setWebViewRect(final int index, final int left, final int top, final int maxWidth, final int maxHeight) {
|
||||||
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
sCocos2dxActivity.runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -148,6 +148,19 @@ namespace experimental{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WebView::setOpacityWebView(float opacity){
|
||||||
|
_impl->setOpacityWebView(opacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
float WebView::getOpacityWebView() const{
|
||||||
|
return _impl->getOpacityWebView();
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebView::setBackgroundTransparent()
|
||||||
|
{
|
||||||
|
_impl->setBackgroundTransparent();
|
||||||
|
};
|
||||||
|
|
||||||
void WebView::onEnter()
|
void WebView::onEnter()
|
||||||
{
|
{
|
||||||
Widget::onEnter();
|
Widget::onEnter();
|
||||||
|
|
|
@ -216,6 +216,20 @@ public:
|
||||||
* Toggle visibility of WebView.
|
* Toggle visibility of WebView.
|
||||||
*/
|
*/
|
||||||
virtual void setVisible(bool visible) override;
|
virtual void setVisible(bool visible) override;
|
||||||
|
/**
|
||||||
|
* SetOpacity of webview.
|
||||||
|
*/
|
||||||
|
virtual void setOpacityWebView(float opacity);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getOpacity of webview.
|
||||||
|
*/
|
||||||
|
virtual float getOpacityWebView() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set the background transparent
|
||||||
|
*/
|
||||||
|
virtual void setBackgroundTransparent();
|
||||||
virtual void onEnter() override;
|
virtual void onEnter() override;
|
||||||
virtual void onExit() override;
|
virtual void onExit() override;
|
||||||
|
|
||||||
|
|
|
@ -286,6 +286,19 @@ namespace cocos2d {
|
||||||
JniHelper::callStaticVoidMethod(className, "setVisible", _viewTag, visible);
|
JniHelper::callStaticVoidMethod(className, "setVisible", _viewTag, visible);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WebViewImpl::setOpacityWebView(const float opacity){
|
||||||
|
JniHelper::callStaticVoidMethod(className, "setOpacityWebView", _viewTag, opacity);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
float WebViewImpl::getOpacityWebView()const{
|
||||||
|
return JniHelper::callStaticFloatMethod(className, "getOpacityWebView", _viewTag);
|
||||||
|
};
|
||||||
|
|
||||||
|
void WebViewImpl::setBackgroundTransparent(){
|
||||||
|
JniHelper::callStaticVoidMethod(className, "setBackgroundTransparent", _viewTag);
|
||||||
|
};
|
||||||
|
|
||||||
void WebViewImpl::setBounces(bool bounces) {
|
void WebViewImpl::setBounces(bool bounces) {
|
||||||
// empty function as this was mainly a fix for iOS
|
// empty function as this was mainly a fix for iOS
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,6 +86,12 @@ namespace cocos2d {
|
||||||
|
|
||||||
void setBounces(bool bounces);
|
void setBounces(bool bounces);
|
||||||
|
|
||||||
|
void setOpacityWebView(float opacity);
|
||||||
|
|
||||||
|
float getOpacityWebView()const;
|
||||||
|
|
||||||
|
void setBackgroundTransparent();
|
||||||
|
|
||||||
static bool shouldStartLoading(const int viewTag, const std::string &url);
|
static bool shouldStartLoading(const int viewTag, const std::string &url);
|
||||||
static void didFinishLoading(const int viewTag, const std::string &url);
|
static void didFinishLoading(const int viewTag, const std::string &url);
|
||||||
static void didFailLoading(const int viewTag, const std::string &url);
|
static void didFailLoading(const int viewTag, const std::string &url);
|
||||||
|
|
|
@ -84,6 +84,12 @@ public:
|
||||||
|
|
||||||
void setBounces(bool bounces);
|
void setBounces(bool bounces);
|
||||||
|
|
||||||
|
virtual void setOpacityWebView(float opacity);
|
||||||
|
|
||||||
|
virtual float getOpacityWebView() const;
|
||||||
|
|
||||||
|
virtual void setBackgroundTransparent();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
UIWebViewWrapper *_uiWebViewWrapper;
|
UIWebViewWrapper *_uiWebViewWrapper;
|
||||||
WebView *_webView;
|
WebView *_webView;
|
||||||
|
|
|
@ -74,6 +74,12 @@ static std::string getFixedBaseUrl(const std::string& baseUrl)
|
||||||
|
|
||||||
- (void)setBounces:(bool)bounces;
|
- (void)setBounces:(bool)bounces;
|
||||||
|
|
||||||
|
- (void)setOpacityWebView:(float)opacity;
|
||||||
|
|
||||||
|
- (float)getOpacityWebView;
|
||||||
|
|
||||||
|
- (void)setBackgroundTransparent;
|
||||||
|
|
||||||
- (void)setFrameWithX:(float)x y:(float)y width:(float)width height:(float)height;
|
- (void)setFrameWithX:(float)x y:(float)y width:(float)width height:(float)height;
|
||||||
|
|
||||||
- (void)setJavascriptInterfaceScheme:(const std::string &)scheme;
|
- (void)setJavascriptInterfaceScheme:(const std::string &)scheme;
|
||||||
|
@ -152,6 +158,19 @@ static std::string getFixedBaseUrl(const std::string& baseUrl)
|
||||||
self.uiWebView.scrollView.bounces = bounces;
|
self.uiWebView.scrollView.bounces = bounces;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)setOpacityWebView:(float)opacity {
|
||||||
|
self.uiWebView.alpha=opacity;
|
||||||
|
[self.uiWebView setOpaque:NO];
|
||||||
|
}
|
||||||
|
|
||||||
|
-(float) getOpacityWebView{
|
||||||
|
return self.uiWebView.alpha;
|
||||||
|
}
|
||||||
|
|
||||||
|
-(void) setBackgroundTransparent{
|
||||||
|
[self.uiWebView setBackgroundColor:[UIColor clearColor]];
|
||||||
|
}
|
||||||
|
|
||||||
- (void)setFrameWithX:(float)x y:(float)y width:(float)width height:(float)height {
|
- (void)setFrameWithX:(float)x y:(float)y width:(float)width height:(float)height {
|
||||||
if (!self.uiWebView) {[self setupWebView];}
|
if (!self.uiWebView) {[self setupWebView];}
|
||||||
CGRect newFrame = CGRectMake(x, y, width, height);
|
CGRect newFrame = CGRectMake(x, y, width, height);
|
||||||
|
@ -232,6 +251,8 @@ static std::string getFixedBaseUrl(const std::string& baseUrl)
|
||||||
self.uiWebView.scalesPageToFit = scalesPageToFit;
|
self.uiWebView.scalesPageToFit = scalesPageToFit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#pragma mark - UIWebViewDelegate
|
#pragma mark - UIWebViewDelegate
|
||||||
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
|
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
|
||||||
NSString *url = [[request URL] absoluteString];
|
NSString *url = [[request URL] absoluteString];
|
||||||
|
@ -398,6 +419,19 @@ void WebViewImpl::setVisible(bool visible){
|
||||||
[_uiWebViewWrapper setVisible:visible];
|
[_uiWebViewWrapper setVisible:visible];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WebViewImpl::setOpacityWebView(float opacity){
|
||||||
|
[_uiWebViewWrapper setOpacityWebView: opacity];
|
||||||
|
}
|
||||||
|
|
||||||
|
float WebViewImpl::getOpacityWebView() const{
|
||||||
|
return [_uiWebViewWrapper getOpacityWebView];
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebViewImpl::setBackgroundTransparent(){
|
||||||
|
[_uiWebViewWrapper setBackgroundTransparent];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace ui
|
} // namespace ui
|
||||||
} // namespace experimental
|
} // namespace experimental
|
||||||
} //namespace cocos2d
|
} //namespace cocos2d
|
||||||
|
|
|
@ -163,6 +163,18 @@ namespace cocos2d {
|
||||||
void WebViewImpl::setBounces(bool bounces) {
|
void WebViewImpl::setBounces(bool bounces) {
|
||||||
// empty function as this was mainly a fix for iOS
|
// empty function as this was mainly a fix for iOS
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WebViewImpl::setOpacityWebView(float opacity){
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
float WebViewImpl::getOpacityWebView() const{
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebViewImpl::setBackgroundTransparent(){
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
} // namespace ui
|
} // namespace ui
|
||||||
} // namespace experimental
|
} // namespace experimental
|
||||||
} //namespace cocos2d
|
} //namespace cocos2d
|
||||||
|
|
|
@ -87,6 +87,12 @@ namespace cocos2d {
|
||||||
|
|
||||||
void setBounces(bool bounces);
|
void setBounces(bool bounces);
|
||||||
|
|
||||||
|
void setOpacityWebView(float opacity);
|
||||||
|
|
||||||
|
float getOpacityWebView() const;
|
||||||
|
|
||||||
|
void setBackgroundTransparent;
|
||||||
|
|
||||||
Evas_Object* _ewkWin;
|
Evas_Object* _ewkWin;
|
||||||
Evas_Object* _ewkView;
|
Evas_Object* _ewkView;
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -50,6 +50,10 @@ bool WebViewTest::init()
|
||||||
|
|
||||||
this->addChild(_webView);
|
this->addChild(_webView);
|
||||||
|
|
||||||
|
auto spriteHello = Sprite::create("Hello.png");
|
||||||
|
spriteHello->setPosition(winSize/2);
|
||||||
|
this->addChild(spriteHello);
|
||||||
|
|
||||||
TextField *urlTextField = TextField::create("Input a URL here", "Arial", 20);
|
TextField *urlTextField = TextField::create("Input a URL here", "Arial", 20);
|
||||||
urlTextField->setPlaceHolderColor(Color3B::RED);
|
urlTextField->setPlaceHolderColor(Color3B::RED);
|
||||||
urlTextField->setPosition(Vec2(winSize/2) + Vec2(-80, _webView->getContentSize().height/2 +
|
urlTextField->setPosition(Vec2(winSize/2) + Vec2(-80, _webView->getContentSize().height/2 +
|
||||||
|
@ -144,6 +148,40 @@ bool WebViewTest::init()
|
||||||
evalJsBtn->setName("evalJs");
|
evalJsBtn->setName("evalJs");
|
||||||
this->addChild(evalJsBtn);
|
this->addChild(evalJsBtn);
|
||||||
|
|
||||||
|
Button *opacityBtn = Button::create("cocosui/animationbuttonnormal.png",
|
||||||
|
"cocosui/animationbuttonpressed.png");
|
||||||
|
opacityBtn->setTitleText("Opacity 1.f");
|
||||||
|
opacityBtn->setPosition(Vec2(winSize/2) - Vec2( _webView->getContentSize().width/2 +
|
||||||
|
opacityBtn->getContentSize().width/2 + 10, 100 ));
|
||||||
|
opacityBtn->addClickEventListener([=](Ref*){
|
||||||
|
auto currentOpacity = _webView->getOpacityWebView();
|
||||||
|
if(currentOpacity ==1.f){
|
||||||
|
_webView->setOpacityWebView(.5f);
|
||||||
|
opacityBtn->setTitleText("Opacity .5f");
|
||||||
|
}else if(currentOpacity == .5f){
|
||||||
|
_webView->setOpacityWebView(0);
|
||||||
|
opacityBtn->setTitleText("Opacity 0.f");
|
||||||
|
}else{
|
||||||
|
_webView->setOpacityWebView(1.f);
|
||||||
|
opacityBtn->setTitleText("Opacity 1.f");
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
opacityBtn->setName("Opacity");
|
||||||
|
this->addChild(opacityBtn);
|
||||||
|
|
||||||
|
|
||||||
|
Button *transparentBgBtn = Button::create("cocosui/animationbuttonnormal.png",
|
||||||
|
"cocosui/animationbuttonpressed.png");
|
||||||
|
transparentBgBtn->setTitleText("Transparent BG");
|
||||||
|
transparentBgBtn->setPosition(Vec2(winSize/2) + Vec2( _webView->getContentSize().width/2 +
|
||||||
|
transparentBgBtn->getContentSize().width/2 + 10,-100 ));
|
||||||
|
transparentBgBtn->addClickEventListener([=](Ref*){
|
||||||
|
_webView->setBackgroundTransparent();
|
||||||
|
});
|
||||||
|
transparentBgBtn->setName("Transparent");
|
||||||
|
this->addChild(transparentBgBtn);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
Loading…
Reference in New Issue